yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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/cloudprovider"
    21  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/auth"
    22  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/auth/credentials"
    23  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/modules"
    24  )
    25  
    26  type Client struct {
    27  	cfg *SClientConfig
    28  	// 标记初始化状态
    29  	init bool
    30  
    31  	Bandwidths           *modules.SBandwidthManager
    32  	Credentials          *modules.SCredentialManager
    33  	Disks                *modules.SDiskManager
    34  	Domains              *modules.SDomainManager
    35  	Eips                 *modules.SEipManager
    36  	Endpoints            *modules.SEndpointManager
    37  	Services             *modules.SServiceManager
    38  	Elasticcache         *modules.SElasticcacheManager
    39  	DcsAvailableZone     *modules.SDcsAvailableZoneManager
    40  	Flavors              *modules.SFlavorManager
    41  	Images               *modules.SImageManager
    42  	OpenStackImages      *modules.SImageManager
    43  	Interface            *modules.SInterfaceManager
    44  	Jobs                 *modules.SJobManager
    45  	Keypairs             *modules.SKeypairManager
    46  	Elb                  *modules.SLoadbalancerManager
    47  	ElbBackend           *modules.SElbBackendManager
    48  	ElbBackendGroup      *modules.SElbBackendGroupManager
    49  	ElbListeners         *modules.SElbListenersManager
    50  	ElbCertificates      *modules.SElbCertificatesManager
    51  	ElbHealthCheck       *modules.SElbHealthCheckManager
    52  	ElbL7policies        *modules.SElbL7policiesManager
    53  	ElbPolicies          *modules.SElbPoliciesManager
    54  	ElbWhitelist         *modules.SElbWhitelistManager
    55  	Port                 *modules.SPortManager
    56  	Projects             *modules.SProjectManager
    57  	Regions              *modules.SRegionManager
    58  	SecurityGroupRules   *modules.SSecgroupRuleManager
    59  	SecurityGroups       *modules.SSecurityGroupManager
    60  	NovaSecurityGroups   *modules.SSecurityGroupManager
    61  	Servers              *modules.SServerManager
    62  	ServersV2            *modules.SServerManager
    63  	NovaServers          *modules.SServerManager
    64  	Snapshots            *modules.SSnapshotManager
    65  	OsSnapshots          *modules.SSnapshotManager
    66  	Subnets              *modules.SSubnetManager
    67  	Users                *modules.SUserManager
    68  	Vpcs                 *modules.SVpcManager
    69  	Zones                *modules.SZoneManager
    70  	VpcRoutes            *modules.SVpcRouteManager
    71  	SNatRules            *modules.SNatSRuleManager
    72  	DNatRules            *modules.SNatDRuleManager
    73  	NatGateways          *modules.SNatGatewayManager
    74  	VpcPeerings          *modules.SVpcPeeringManager
    75  	DBInstance           *modules.SDBInstanceManager
    76  	DBInstanceBackup     *modules.SDBInstanceBackupManager
    77  	DBInstanceFlavor     *modules.SDBInstanceFlavorManager
    78  	DBInstanceDatastore  *modules.SDBInstanceDatastoreManager
    79  	DBInstanceStorage    *modules.SDBInstanceStorageManager
    80  	DBInstanceJob        *modules.SDBInstanceJobManager
    81  	Traces               *modules.STraceManager
    82  	CloudEye             *modules.SCloudEyeManager
    83  	Quotas               *modules.SQuotaManager
    84  	EnterpriseProjects   *modules.SEnterpriseProjectManager
    85  	Roles                *modules.SRoleManager
    86  	Groups               *modules.SGroupManager
    87  	SAMLProviders        *modules.SAMLProviderManager
    88  	SAMLProviderMappings *modules.SAMLProviderMappingManager
    89  	SfsTurbos            *modules.SfsTurboManager
    90  }
    91  
    92  type SClientConfig struct {
    93  	signer        auth.Signer
    94  	endpoints     *cloudprovider.SHCSOEndpoints
    95  	regionId      string
    96  	domainId      string
    97  	projectId     string
    98  	defaultRegion string
    99  
   100  	debug bool
   101  }
   102  
   103  func (self *SClientConfig) GetSigner() auth.Signer {
   104  	return self.signer
   105  }
   106  
   107  func (self *SClientConfig) GetEndpoints() *cloudprovider.SHCSOEndpoints {
   108  	return self.endpoints
   109  }
   110  
   111  func (self *SClientConfig) GetDefaultRegion() string {
   112  	return self.defaultRegion
   113  }
   114  
   115  func (self *SClientConfig) GetRegionId() string {
   116  	return self.regionId
   117  }
   118  
   119  func (self *SClientConfig) GetDomainId() string {
   120  	return self.domainId
   121  }
   122  
   123  func (self *SClientConfig) GetProjectId() string {
   124  	return self.projectId
   125  }
   126  
   127  func (self *SClientConfig) GetDebug() bool {
   128  	return self.debug
   129  }
   130  
   131  func (self *Client) SetHttpClient(httpClient *http.Client) {
   132  	self.Credentials.SetHttpClient(httpClient)
   133  	self.Servers.SetHttpClient(httpClient)
   134  	self.ServersV2.SetHttpClient(httpClient)
   135  	self.NovaServers.SetHttpClient(httpClient)
   136  	self.Snapshots.SetHttpClient(httpClient)
   137  	self.OsSnapshots.SetHttpClient(httpClient)
   138  	self.Images.SetHttpClient(httpClient)
   139  	self.OpenStackImages.SetHttpClient(httpClient)
   140  	self.Projects.SetHttpClient(httpClient)
   141  	self.Regions.SetHttpClient(httpClient)
   142  	self.Zones.SetHttpClient(httpClient)
   143  	self.Vpcs.SetHttpClient(httpClient)
   144  	self.Eips.SetHttpClient(httpClient)
   145  	self.Elasticcache.SetHttpClient(httpClient)
   146  	self.DcsAvailableZone.SetHttpClient(httpClient)
   147  	self.Disks.SetHttpClient(httpClient)
   148  	self.Domains.SetHttpClient(httpClient)
   149  	self.Keypairs.SetHttpClient(httpClient)
   150  	self.Elb.SetHttpClient(httpClient)
   151  	self.ElbBackend.SetHttpClient(httpClient)
   152  	self.ElbBackendGroup.SetHttpClient(httpClient)
   153  	self.ElbListeners.SetHttpClient(httpClient)
   154  	self.ElbCertificates.SetHttpClient(httpClient)
   155  	self.ElbHealthCheck.SetHttpClient(httpClient)
   156  	self.ElbL7policies.SetHttpClient(httpClient)
   157  	self.ElbPolicies.SetHttpClient(httpClient)
   158  	self.ElbWhitelist.SetHttpClient(httpClient)
   159  	self.SecurityGroupRules.SetHttpClient(httpClient)
   160  	self.SecurityGroups.SetHttpClient(httpClient)
   161  	self.NovaSecurityGroups.SetHttpClient(httpClient)
   162  	self.Subnets.SetHttpClient(httpClient)
   163  	self.Users.SetHttpClient(httpClient)
   164  	self.Interface.SetHttpClient(httpClient)
   165  	self.Jobs.SetHttpClient(httpClient)
   166  	self.Bandwidths.SetHttpClient(httpClient)
   167  	self.Port.SetHttpClient(httpClient)
   168  	self.Flavors.SetHttpClient(httpClient)
   169  	self.VpcRoutes.SetHttpClient(httpClient)
   170  	self.SNatRules.SetHttpClient(httpClient)
   171  	self.DNatRules.SetHttpClient(httpClient)
   172  	self.NatGateways.SetHttpClient(httpClient)
   173  	self.DBInstance.SetHttpClient(httpClient)
   174  	self.DBInstanceBackup.SetHttpClient(httpClient)
   175  	self.DBInstanceFlavor.SetHttpClient(httpClient)
   176  	self.DBInstanceJob.SetHttpClient(httpClient)
   177  	self.Traces.SetHttpClient(httpClient)
   178  	self.CloudEye.SetHttpClient(httpClient)
   179  	self.EnterpriseProjects.SetHttpClient(httpClient)
   180  	self.Roles.SetHttpClient(httpClient)
   181  	self.Groups.SetHttpClient(httpClient)
   182  	self.SAMLProviders.SetHttpClient(httpClient)
   183  	self.SAMLProviderMappings.SetHttpClient(httpClient)
   184  	self.SfsTurbos.SetHttpClient(httpClient)
   185  	self.Endpoints.SetHttpClient(httpClient)
   186  	self.Services.SetHttpClient(httpClient)
   187  }
   188  
   189  func (self *Client) InitWithAccessKey(regionId, domainId, projectId, accessKey, secretKey string, debug bool, defaultRegion string, endpoints *cloudprovider.SHCSOEndpoints) error {
   190  	// accessKey signer
   191  	credential := &credentials.AccessKeyCredential{
   192  		AccessKeyId:     accessKey,
   193  		AccessKeySecret: secretKey,
   194  	}
   195  
   196  	// 从signer中初始化
   197  	signer, err := auth.NewSignerWithCredential(credential)
   198  	if err != nil {
   199  		return err
   200  	}
   201  	self.cfg = &SClientConfig{
   202  		signer:        signer,
   203  		endpoints:     endpoints,
   204  		regionId:      regionId,
   205  		defaultRegion: defaultRegion,
   206  		domainId:      domainId,
   207  		projectId:     projectId,
   208  		debug:         debug,
   209  	}
   210  
   211  	// 初始化 resource manager
   212  	self.initManagers()
   213  	return err
   214  }
   215  
   216  func (self *Client) initManagers() {
   217  	if !self.init {
   218  		self.Servers = modules.NewServerManager(self.cfg)
   219  		self.ServersV2 = modules.NewServerV2Manager(self.cfg)
   220  		self.NovaServers = modules.NewNovaServerManager(self.cfg)
   221  		self.Snapshots = modules.NewSnapshotManager(self.cfg)
   222  		self.OsSnapshots = modules.NewOsSnapshotManager(self.cfg)
   223  		self.Images = modules.NewImageManager(self.cfg)
   224  		self.OpenStackImages = modules.NewOpenstackImageManager(self.cfg)
   225  		self.Projects = modules.NewProjectManager(self.cfg)
   226  		self.Regions = modules.NewRegionManager(self.cfg)
   227  		self.Zones = modules.NewZoneManager(self.cfg)
   228  		self.Vpcs = modules.NewVpcManager(self.cfg)
   229  		self.Eips = modules.NewEipManager(self.cfg)
   230  		self.Elasticcache = modules.NewElasticcacheManager(self.cfg)
   231  		self.DcsAvailableZone = modules.NewDcsAvailableZoneManager(self.cfg)
   232  		self.Disks = modules.NewDiskManager(self.cfg)
   233  		self.Domains = modules.NewDomainManager(self.cfg)
   234  		self.Keypairs = modules.NewKeypairManager(self.cfg)
   235  		self.Elb = modules.NewLoadbalancerManager(self.cfg)
   236  		self.ElbBackend = modules.NewElbBackendManager(self.cfg)
   237  		self.ElbBackendGroup = modules.NewElbBackendGroupManager(self.cfg)
   238  		self.ElbListeners = modules.NewElbListenersManager(self.cfg)
   239  		self.ElbCertificates = modules.NewElbCertificatesManager(self.cfg)
   240  		self.ElbHealthCheck = modules.NewElbHealthCheckManager(self.cfg)
   241  		self.ElbL7policies = modules.NewElbL7policiesManager(self.cfg)
   242  		self.ElbPolicies = modules.NewElbPoliciesManager(self.cfg)
   243  		self.ElbWhitelist = modules.NewElbWhitelistManager(self.cfg)
   244  		self.SecurityGroupRules = modules.NewSecgroupRuleManager(self.cfg)
   245  		self.SecurityGroups = modules.NewSecurityGroupManager(self.cfg)
   246  		self.NovaSecurityGroups = modules.NewNovaSecurityGroupManager(self.cfg)
   247  		self.Subnets = modules.NewSubnetManager(self.cfg)
   248  		self.Users = modules.NewUserManager(self.cfg)
   249  		self.Interface = modules.NewInterfaceManager(self.cfg)
   250  		self.Jobs = modules.NewJobManager(self.cfg)
   251  		self.Bandwidths = modules.NewBandwidthManager(self.cfg)
   252  		self.Credentials = modules.NewCredentialManager(self.cfg)
   253  		self.Port = modules.NewPortManager(self.cfg)
   254  		self.Flavors = modules.NewFlavorManager(self.cfg)
   255  		self.VpcRoutes = modules.NewVpcRouteManager(self.cfg)
   256  		self.SNatRules = modules.NewNatSManager(self.cfg)
   257  		self.DNatRules = modules.NewNatDManager(self.cfg)
   258  		self.NatGateways = modules.NewNatGatewayManager(self.cfg)
   259  		self.VpcPeerings = modules.NewVpcPeeringManager(self.cfg)
   260  		self.DBInstance = modules.NewDBInstanceManager(self.cfg)
   261  		self.DBInstanceBackup = modules.NewDBInstanceBackupManager(self.cfg)
   262  		self.DBInstanceFlavor = modules.NewDBInstanceFlavorManager(self.cfg)
   263  		self.DBInstanceStorage = modules.NewDBInstanceStorageManager(self.cfg)
   264  		self.DBInstanceDatastore = modules.NewDBInstanceDatastoreManager(self.cfg)
   265  		self.DBInstanceJob = modules.NewDBInstanceJobManager(self.cfg)
   266  		self.Traces = modules.NewTraceManager(self.cfg)
   267  		self.CloudEye = modules.NewCloudEyeManager(self.cfg)
   268  		self.Quotas = modules.NewQuotaManager(self.cfg)
   269  		self.EnterpriseProjects = modules.NewEnterpriseProjectManager(self.cfg)
   270  		self.Roles = modules.NewRoleManager(self.cfg)
   271  		self.Groups = modules.NewGroupManager(self.cfg)
   272  		self.SAMLProviders = modules.NewSAMLProviderManager(self.cfg)
   273  		self.SAMLProviderMappings = modules.NewSAMLProviderMappingManager(self.cfg)
   274  		self.SfsTurbos = modules.NewSfsTurboManager(self.cfg)
   275  		self.Endpoints = modules.NewEndpointManager(self.cfg)
   276  		self.Services = modules.NewServiceManager(self.cfg)
   277  	}
   278  
   279  	self.init = true
   280  }
   281  
   282  func NewClientWithAccessKey(regionId, domainId, projectId, accessKey, secretKey string, debug bool, defaultRegion string, endpoints *cloudprovider.SHCSOEndpoints) (*Client, error) {
   283  	c := &Client{}
   284  	err := c.InitWithAccessKey(regionId, domainId, projectId, accessKey, secretKey, debug, defaultRegion, endpoints)
   285  	return c, err
   286  }