From efd4fc1ef2923c636e075919697e9b45be48efb1 Mon Sep 17 00:00:00 2001 From: Rain Date: Fri, 3 Jan 2020 15:56:00 +0800 Subject: [PATCH] fix(huawei): Add 'X-Project-ID' header for huawei nat reuqest --- .../huawei/client/modules/mod_dnat_rules.go | 6 ++++- .../huawei/client/modules/mod_natgateway.go | 15 ++++++++++++- .../huawei/client/modules/mod_snat_rules.go | 6 ++++- pkg/multicloud/huawei/natgateway.go | 22 +++++-------------- pkg/multicloud/huawei/shell/natgateway.go | 3 ++- pkg/multicloud/huawei/vpc.go | 2 +- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/pkg/multicloud/huawei/client/modules/mod_dnat_rules.go b/pkg/multicloud/huawei/client/modules/mod_dnat_rules.go index 96af2e8ea7..4b64531403 100644 --- a/pkg/multicloud/huawei/client/modules/mod_dnat_rules.go +++ b/pkg/multicloud/huawei/client/modules/mod_dnat_rules.go @@ -23,7 +23,7 @@ type SNatDRuleManager struct { } func NewNatDManager(regionId string, projectId string, signer auth.Signer, debug bool) *SNatDRuleManager { - return &SNatDRuleManager{SResourceManager: SResourceManager{ + man := &SNatDRuleManager{SResourceManager: SResourceManager{ SBaseManager: NewBaseManager(signer, debug), ServiceName: ServiceNameNAT, Region: regionId, @@ -34,4 +34,8 @@ func NewNatDManager(regionId string, projectId string, signer auth.Signer, debug ResourceKeyword: "dnat_rules", }} + if len(projectId) > 0 { + man.requestHook = &sProjectHook{projectId} + } + return man } diff --git a/pkg/multicloud/huawei/client/modules/mod_natgateway.go b/pkg/multicloud/huawei/client/modules/mod_natgateway.go index 7bc23c58a2..17b2898998 100644 --- a/pkg/multicloud/huawei/client/modules/mod_natgateway.go +++ b/pkg/multicloud/huawei/client/modules/mod_natgateway.go @@ -16,14 +16,23 @@ package modules import ( "yunion.io/x/onecloud/pkg/multicloud/huawei/client/auth" + "yunion.io/x/onecloud/pkg/multicloud/huawei/client/requests" ) type SNatGatewayManager struct { SResourceManager } +type sProjectHook struct { + projectId string +} + +func (self *sProjectHook) Process(request requests.IRequest) { + request.AddHeaderParam("X-Project-Id", self.projectId) +} + func NewNatGatewayManager(regionId string, projectId string, signer auth.Signer, debug bool) *SNatGatewayManager { - return &SNatGatewayManager{SResourceManager: SResourceManager{ + man := &SNatGatewayManager{SResourceManager: SResourceManager{ SBaseManager: NewBaseManager(signer, debug), ServiceName: ServiceNameNAT, Region: regionId, @@ -34,4 +43,8 @@ func NewNatGatewayManager(regionId string, projectId string, signer auth.Signer, ResourceKeyword: "nat_gateways", }} + if len(projectId) > 0 { + man.requestHook = &sProjectHook{projectId} + } + return man } diff --git a/pkg/multicloud/huawei/client/modules/mod_snat_rules.go b/pkg/multicloud/huawei/client/modules/mod_snat_rules.go index 213c8b0e2e..701e7e56ab 100644 --- a/pkg/multicloud/huawei/client/modules/mod_snat_rules.go +++ b/pkg/multicloud/huawei/client/modules/mod_snat_rules.go @@ -23,7 +23,7 @@ type SNatSRuleManager struct { } func NewNatSManager(regionId string, projectId string, signer auth.Signer, debug bool) *SNatSRuleManager { - return &SNatSRuleManager{SResourceManager: SResourceManager{ + man := &SNatSRuleManager{SResourceManager: SResourceManager{ SBaseManager: NewBaseManager(signer, debug), ServiceName: ServiceNameNAT, Region: regionId, @@ -34,4 +34,8 @@ func NewNatSManager(regionId string, projectId string, signer auth.Signer, debug ResourceKeyword: "snat_rules", }} + if len(projectId) > 0 { + man.requestHook = &sProjectHook{projectId} + } + return man } diff --git a/pkg/multicloud/huawei/natgateway.go b/pkg/multicloud/huawei/natgateway.go index 02fec89cf2..42bf056968 100644 --- a/pkg/multicloud/huawei/natgateway.go +++ b/pkg/multicloud/huawei/natgateway.go @@ -171,11 +171,14 @@ func (gateway *SNatGateway) GetINatSEntryByID(id string) (cloudprovider.ICloudNa return &snat, nil } -func (region *SRegion) GetNatGateway(natGatewayID string) ([]SNatGateway, error) { +func (region *SRegion) GetNatGateways(vpcID, natGatewayID string) ([]SNatGateway, error) { queues := make(map[string]string) - if natGatewayID != "" { + if len(natGatewayID) != 0 { queues["id"] = natGatewayID } + if len(vpcID) != 0 { + queues["router_id"] = vpcID + } natGateways := make([]SNatGateway, 0, 2) err := doListAllWithMarker(region.ecsClient.NatGateways.List, queues, &natGateways) if err != nil { @@ -187,21 +190,6 @@ func (region *SRegion) GetNatGateway(natGatewayID string) ([]SNatGateway, error) return natGateways, nil } -func (region *SRegion) GetNatGateways(vpcID string) ([]SNatGateway, error) { - queues := map[string]string{ - "router_id": vpcID, - } - natGateways := make([]SNatGateway, 0, 2) - err := doListAllWithMarker(region.ecsClient.NatGateways.List, queues, &natGateways) - if err != nil { - return nil, errors.Wrapf(err, "get nat gateways error by vpcid") - } - for i := range natGateways { - natGateways[i].region = region - } - return natGateways, nil -} - func (region *SRegion) CreateNatDEntry(rule cloudprovider.SNatDRule, gatewayID string) (SNatDEntry, error) { params := make(map[string]interface{}) params["nat_gateway_id"] = gatewayID diff --git a/pkg/multicloud/huawei/shell/natgateway.go b/pkg/multicloud/huawei/shell/natgateway.go index d34d3499d1..0bf1d71199 100644 --- a/pkg/multicloud/huawei/shell/natgateway.go +++ b/pkg/multicloud/huawei/shell/natgateway.go @@ -23,9 +23,10 @@ import ( func init() { type NatGatewayOptions struct { NatGatewayID string `help:"Nat Gateway ID"` + VpcID string `help:"Vpc ID"` } shellutils.R(&NatGatewayOptions{}, "nat-gateway-list", "List nat gateway", func(region *huawei.SRegion, args *NatGatewayOptions) error { - natGateways, err := region.GetNatGateway(args.NatGatewayID) + natGateways, err := region.GetNatGateways(args.VpcID, args.NatGatewayID) if err != nil { return err } diff --git a/pkg/multicloud/huawei/vpc.go b/pkg/multicloud/huawei/vpc.go index ae416475d6..6777e83dea 100644 --- a/pkg/multicloud/huawei/vpc.go +++ b/pkg/multicloud/huawei/vpc.go @@ -206,7 +206,7 @@ func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) } func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) { - nats, err := self.region.GetNatGateways(self.GetId()) + nats, err := self.region.GetNatGateways(self.GetId(), "") if err != nil { return nil, err }