yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/client/client.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import (
    18  	"net/http"
    19  
    20  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/auth"
    21  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/auth/credentials"
    22  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/modules"
    23  )
    24  
    25  type Client struct {
    26  	cfg *SClientConfig
    27  	// 标记初始化状态
    28  	init bool
    29  
    30  	Balances             *modules.SBalanceManager
    31  	Credentials          *modules.SCredentialManager
    32  	Disks                *modules.SDiskManager
    33  	Domains              *modules.SDomainManager
    34  	Elasticcache         *modules.SElasticcacheManager
    35  	DcsAvailableZone     *modules.SDcsAvailableZoneManager
    36  	Flavors              *modules.SFlavorManager
    37  	Images               *modules.SImageManager
    38  	OpenStackImages      *modules.SImageManager
    39  	Interface            *modules.SInterfaceManager
    40  	Jobs                 *modules.SJobManager
    41  	Keypairs             *modules.SKeypairManager
    42  	Orders               *modules.SOrderManager
    43  	Port                 *modules.SPortManager
    44  	Projects             *modules.SProjectManager
    45  	Regions              *modules.SRegionManager
    46  	SecurityGroupRules   *modules.SSecgroupRuleManager
    47  	SecurityGroups       *modules.SSecurityGroupManager
    48  	NovaSecurityGroups   *modules.SSecurityGroupManager
    49  	Servers              *modules.SServerManager
    50  	ServersV2            *modules.SServerManager
    51  	NovaServers          *modules.SServerManager
    52  	Snapshots            *modules.SSnapshotManager
    53  	OsSnapshots          *modules.SSnapshotManager
    54  	Subnets              *modules.SSubnetManager
    55  	Users                *modules.SUserManager
    56  	Vpcs                 *modules.SVpcManager
    57  	Zones                *modules.SZoneManager
    58  	VpcRoutes            *modules.SVpcRouteManager
    59  	SNatRules            *modules.SNatSRuleManager
    60  	DNatRules            *modules.SNatDRuleManager
    61  	NatGateways          *modules.SNatGatewayManager
    62  	VpcPeerings          *modules.SVpcPeeringManager
    63  	DBInstance           *modules.SDBInstanceManager
    64  	DBInstanceBackup     *modules.SDBInstanceBackupManager
    65  	DBInstanceFlavor     *modules.SDBInstanceFlavorManager
    66  	DBInstanceJob        *modules.SDBInstanceJobManager
    67  	Traces               *modules.STraceManager
    68  	CloudEye             *modules.SCloudEyeManager
    69  	Quotas               *modules.SQuotaManager
    70  	EnterpriseProjects   *modules.SEnterpriseProjectManager
    71  	Roles                *modules.SRoleManager
    72  	Groups               *modules.SGroupManager
    73  	SAMLProviders        *modules.SAMLProviderManager
    74  	SAMLProviderMappings *modules.SAMLProviderMappingManager
    75  	SfsTurbos            *modules.SfsTurboManager
    76  	RouteTables          *modules.SRouteTableManager
    77  }
    78  
    79  type SClientConfig struct {
    80  	signer    auth.Signer
    81  	endpoint  string // myhuaweicloud.com
    82  	regionId  string
    83  	domainId  string
    84  	projectId string
    85  
    86  	debug bool
    87  }
    88  
    89  func (self *SClientConfig) GetSigner() auth.Signer {
    90  	return self.signer
    91  }
    92  
    93  func (self *SClientConfig) GetEndpoint() string {
    94  	return self.endpoint
    95  }
    96  
    97  func (self *SClientConfig) GetRegionId() string {
    98  	return self.regionId
    99  }
   100  
   101  func (self *SClientConfig) GetDomainId() string {
   102  	return self.domainId
   103  }
   104  
   105  func (self *SClientConfig) GetProjectId() string {
   106  	return self.projectId
   107  }
   108  
   109  func (self *SClientConfig) GetDebug() bool {
   110  	return self.debug
   111  }
   112  
   113  func (self *Client) SetHttpClient(httpClient *http.Client) {
   114  	self.Credentials.SetHttpClient(httpClient)
   115  	self.Servers.SetHttpClient(httpClient)
   116  	self.ServersV2.SetHttpClient(httpClient)
   117  	self.NovaServers.SetHttpClient(httpClient)
   118  	self.Snapshots.SetHttpClient(httpClient)
   119  	self.OsSnapshots.SetHttpClient(httpClient)
   120  	self.Images.SetHttpClient(httpClient)
   121  	self.OpenStackImages.SetHttpClient(httpClient)
   122  	self.Projects.SetHttpClient(httpClient)
   123  	self.Regions.SetHttpClient(httpClient)
   124  	self.Zones.SetHttpClient(httpClient)
   125  	self.Vpcs.SetHttpClient(httpClient)
   126  	self.Elasticcache.SetHttpClient(httpClient)
   127  	self.DcsAvailableZone.SetHttpClient(httpClient)
   128  	self.Disks.SetHttpClient(httpClient)
   129  	self.Domains.SetHttpClient(httpClient)
   130  	self.Keypairs.SetHttpClient(httpClient)
   131  	self.Orders.SetHttpClient(httpClient)
   132  	self.SecurityGroupRules.SetHttpClient(httpClient)
   133  	self.SecurityGroups.SetHttpClient(httpClient)
   134  	self.NovaSecurityGroups.SetHttpClient(httpClient)
   135  	self.Subnets.SetHttpClient(httpClient)
   136  	self.Users.SetHttpClient(httpClient)
   137  	self.Interface.SetHttpClient(httpClient)
   138  	self.Jobs.SetHttpClient(httpClient)
   139  	self.Balances.SetHttpClient(httpClient)
   140  	self.Port.SetHttpClient(httpClient)
   141  	self.Flavors.SetHttpClient(httpClient)
   142  	self.VpcRoutes.SetHttpClient(httpClient)
   143  	self.SNatRules.SetHttpClient(httpClient)
   144  	self.DNatRules.SetHttpClient(httpClient)
   145  	self.NatGateways.SetHttpClient(httpClient)
   146  	self.DBInstance.SetHttpClient(httpClient)
   147  	self.DBInstanceBackup.SetHttpClient(httpClient)
   148  	self.DBInstanceFlavor.SetHttpClient(httpClient)
   149  	self.DBInstanceJob.SetHttpClient(httpClient)
   150  	self.Traces.SetHttpClient(httpClient)
   151  	self.CloudEye.SetHttpClient(httpClient)
   152  	self.EnterpriseProjects.SetHttpClient(httpClient)
   153  	self.Roles.SetHttpClient(httpClient)
   154  	self.Groups.SetHttpClient(httpClient)
   155  	self.SAMLProviders.SetHttpClient(httpClient)
   156  	self.SAMLProviderMappings.SetHttpClient(httpClient)
   157  	self.SfsTurbos.SetHttpClient(httpClient)
   158  	self.RouteTables.SetHttpClient(httpClient)
   159  }
   160  
   161  func (self *Client) InitWithAccessKey(endpoint, regionId, domainId, projectId, accessKey, secretKey string, debug bool) error {
   162  	// accessKey signer
   163  	credential := &credentials.AccessKeyCredential{
   164  		AccessKeyId:     accessKey,
   165  		AccessKeySecret: secretKey,
   166  	}
   167  
   168  	// 从signer中初始化
   169  	signer, err := auth.NewSignerWithCredential(credential)
   170  	if err != nil {
   171  		return err
   172  	}
   173  	self.cfg = &SClientConfig{
   174  		signer:    signer,
   175  		endpoint:  endpoint,
   176  		regionId:  regionId,
   177  		domainId:  domainId,
   178  		projectId: projectId,
   179  		debug:     debug,
   180  	}
   181  
   182  	// 初始化 resource manager
   183  	self.initManagers()
   184  	return err
   185  }
   186  
   187  func (self *Client) initManagers() {
   188  	if !self.init {
   189  		self.Servers = modules.NewServerManager(self.cfg)
   190  		self.ServersV2 = modules.NewServerV2Manager(self.cfg)
   191  		self.NovaServers = modules.NewNovaServerManager(self.cfg)
   192  		self.Snapshots = modules.NewSnapshotManager(self.cfg)
   193  		self.OsSnapshots = modules.NewOsSnapshotManager(self.cfg)
   194  		self.Images = modules.NewImageManager(self.cfg)
   195  		self.OpenStackImages = modules.NewOpenstackImageManager(self.cfg)
   196  		self.Projects = modules.NewProjectManager(self.cfg)
   197  		self.Regions = modules.NewRegionManager(self.cfg)
   198  		self.Zones = modules.NewZoneManager(self.cfg)
   199  		self.Vpcs = modules.NewVpcManager(self.cfg)
   200  		self.Elasticcache = modules.NewElasticcacheManager(self.cfg)
   201  		self.DcsAvailableZone = modules.NewDcsAvailableZoneManager(self.cfg)
   202  		self.Disks = modules.NewDiskManager(self.cfg)
   203  		self.Domains = modules.NewDomainManager(self.cfg)
   204  		self.Keypairs = modules.NewKeypairManager(self.cfg)
   205  		self.Orders = modules.NewOrderManager(self.cfg)
   206  		self.SecurityGroupRules = modules.NewSecgroupRuleManager(self.cfg)
   207  		self.SecurityGroups = modules.NewSecurityGroupManager(self.cfg)
   208  		self.NovaSecurityGroups = modules.NewNovaSecurityGroupManager(self.cfg)
   209  		self.Subnets = modules.NewSubnetManager(self.cfg)
   210  		self.Users = modules.NewUserManager(self.cfg)
   211  		self.Interface = modules.NewInterfaceManager(self.cfg)
   212  		self.Jobs = modules.NewJobManager(self.cfg)
   213  		self.Balances = modules.NewBalanceManager(self.cfg)
   214  		self.Credentials = modules.NewCredentialManager(self.cfg)
   215  		self.Port = modules.NewPortManager(self.cfg)
   216  		self.Flavors = modules.NewFlavorManager(self.cfg)
   217  		self.VpcRoutes = modules.NewVpcRouteManager(self.cfg)
   218  		self.SNatRules = modules.NewNatSManager(self.cfg)
   219  		self.DNatRules = modules.NewNatDManager(self.cfg)
   220  		self.NatGateways = modules.NewNatGatewayManager(self.cfg)
   221  		self.VpcPeerings = modules.NewVpcPeeringManager(self.cfg)
   222  		self.DBInstance = modules.NewDBInstanceManager(self.cfg)
   223  		self.DBInstanceBackup = modules.NewDBInstanceBackupManager(self.cfg)
   224  		self.DBInstanceFlavor = modules.NewDBInstanceFlavorManager(self.cfg)
   225  		self.DBInstanceJob = modules.NewDBInstanceJobManager(self.cfg)
   226  		self.Traces = modules.NewTraceManager(self.cfg)
   227  		self.CloudEye = modules.NewCloudEyeManager(self.cfg)
   228  		self.Quotas = modules.NewQuotaManager(self.cfg)
   229  		self.EnterpriseProjects = modules.NewEnterpriseProjectManager(self.cfg)
   230  		self.Roles = modules.NewRoleManager(self.cfg)
   231  		self.Groups = modules.NewGroupManager(self.cfg)
   232  		self.SAMLProviders = modules.NewSAMLProviderManager(self.cfg)
   233  		self.SAMLProviderMappings = modules.NewSAMLProviderMappingManager(self.cfg)
   234  		self.SfsTurbos = modules.NewSfsTurboManager(self.cfg)
   235  		self.RouteTables = modules.NewRouteTableManager(self.cfg)
   236  	}
   237  
   238  	self.init = true
   239  }
   240  
   241  func NewClientWithAccessKey(endpoint, regionId, domainId, projectId, accessKey, secretKey string, debug bool) (*Client, error) {
   242  	c := &Client{}
   243  	err := c.InitWithAccessKey(endpoint, regionId, domainId, projectId, accessKey, secretKey, debug)
   244  	return c, err
   245  }
   246  
   247  func NewPublicCloudClientWithAccessKey(regionId, domainId, projectId, accessKey, secretKey string, debug bool) (*Client, error) {
   248  	return NewClientWithAccessKey("myhuaweicloud.com", regionId, domainId, projectId, accessKey, secretKey, debug)
   249  }