google: use GoogleClientConfig

This commit is contained in:
Yousong Zhou
2020-03-22 19:36:43 +08:00
parent ce0e9dd689
commit 554e2e88dc
5 changed files with 87 additions and 55 deletions
+69 -49
View File
@@ -69,13 +69,41 @@ var (
DualRegions []string = []string{"nam4", "eur4"}
)
type GoogleClientConfig struct {
cpcfg cloudprovider.ProviderConfig
projectId string
clientEmail string
privateKeyId string
privateKey string
debug bool
}
func NewGoogleClientConfig(projectId, clientEmail, privateKeyId, privateKey string) *GoogleClientConfig {
privateKey = strings.Replace(privateKey, "\\n", "\n", -1)
cfg := &GoogleClientConfig{
projectId: projectId,
clientEmail: clientEmail,
privateKeyId: privateKeyId,
privateKey: privateKey,
}
return cfg
}
func (cfg *GoogleClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *GoogleClientConfig {
cfg.cpcfg = cpcfg
return cfg
}
func (cfg *GoogleClientConfig) Debug(debug bool) *GoogleClientConfig {
cfg.debug = debug
return cfg
}
type SGoogleClient struct {
providerId string
providerName string
projectId string
privateKey string
privateKeyId string
clientEmail string
*GoogleClientConfig
iregions []cloudprovider.ICloudRegion
images []SImage
snapshots map[string][]SSnapshot
@@ -84,24 +112,16 @@ type SGoogleClient struct {
client *http.Client
iBuckets []cloudprovider.ICloudBucket
Debug bool
}
func NewGoogleClient(providerId string, providerName string, projectId, clientEmail, privateKeyId, privateKey string, isDebug bool) (*SGoogleClient, error) {
func NewGoogleClient(cfg *GoogleClientConfig) (*SGoogleClient, error) {
client := SGoogleClient{
providerId: providerId,
providerName: providerName,
projectId: projectId,
privateKey: strings.Replace(privateKey, "\\n", "\n", -1),
privateKeyId: privateKeyId,
clientEmail: clientEmail,
Debug: isDebug,
GoogleClientConfig: cfg,
}
conf := &jwt.Config{
Email: clientEmail,
PrivateKeyID: privateKeyId,
PrivateKey: []byte(client.privateKey),
Email: cfg.clientEmail,
PrivateKeyID: cfg.privateKeyId,
PrivateKey: []byte(cfg.privateKey),
Scopes: []string{
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/compute",
@@ -207,7 +227,7 @@ func jsonRequest(client *http.Client, method httputils.THttpMethod, domain, apiV
}
func (self *SGoogleClient) ecsGet(resource string, retval interface{}) error {
resp, err := jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, nil, nil, self.Debug)
resp, err := jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, nil, nil, self.debug)
if err != nil {
return err
}
@@ -222,15 +242,15 @@ func (self *SGoogleClient) ecsGet(resource string, retval interface{}) error {
func (self *SGoogleClient) ecsList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
resource = fmt.Sprintf("projects/%s/%s", self.projectId, resource)
return jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, nil, self.Debug)
return jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, nil, self.debug)
}
func (self *SGoogleClient) managerList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
return jsonRequest(self.client, "GET", GOOGLE_MANAGER_DOMAIN, GOOGLE_MANAGER_API_VERSION, resource, params, nil, self.Debug)
return jsonRequest(self.client, "GET", GOOGLE_MANAGER_DOMAIN, GOOGLE_MANAGER_API_VERSION, resource, params, nil, self.debug)
}
func (self *SGoogleClient) managerGet(resource string) (jsonutils.JSONObject, error) {
return jsonRequest(self.client, "GET", GOOGLE_MANAGER_DOMAIN, GOOGLE_MANAGER_API_VERSION, resource, nil, nil, self.Debug)
return jsonRequest(self.client, "GET", GOOGLE_MANAGER_DOMAIN, GOOGLE_MANAGER_API_VERSION, resource, nil, nil, self.debug)
}
func (self *SGoogleClient) ecsListAll(resource string, params map[string]string, retval interface{}) error {
@@ -262,7 +282,7 @@ func (self *SGoogleClient) ecsListAll(resource string, params map[string]string,
}
func (self *SGoogleClient) ecsDelete(id string, retval interface{}) error {
resp, err := jsonRequest(self.client, "DELETE", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, id, nil, nil, self.Debug)
resp, err := jsonRequest(self.client, "DELETE", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, id, nil, nil, self.debug)
if err != nil {
return err
}
@@ -276,7 +296,7 @@ func (self *SGoogleClient) ecsPatch(resource string, action string, params map[s
if len(action) > 0 {
resource = fmt.Sprintf("%s/%s", resource, action)
}
resp, err := jsonRequest(self.client, "PATCH", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, body, self.Debug)
resp, err := jsonRequest(self.client, "PATCH", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, body, self.debug)
if err != nil {
return "", err
}
@@ -286,7 +306,7 @@ func (self *SGoogleClient) ecsPatch(resource string, action string, params map[s
func (self *SGoogleClient) ecsDo(resource string, action string, params map[string]string, body jsonutils.JSONObject) (string, error) {
resource = fmt.Sprintf("%s/%s", resource, action)
resp, err := jsonRequest(self.client, "POST", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, body, self.Debug)
resp, err := jsonRequest(self.client, "POST", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, body, self.debug)
if err != nil {
return "", err
}
@@ -295,7 +315,7 @@ func (self *SGoogleClient) ecsDo(resource string, action string, params map[stri
}
func (self *SGoogleClient) rdsDelete(id string, retval interface{}) error {
resp, err := jsonRequest(self.client, "DELETE", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, id, nil, nil, self.Debug)
resp, err := jsonRequest(self.client, "DELETE", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, id, nil, nil, self.debug)
if err != nil {
return err
}
@@ -307,7 +327,7 @@ func (self *SGoogleClient) rdsDelete(id string, retval interface{}) error {
func (self *SGoogleClient) rdsDo(resource string, action string, params map[string]string, body jsonutils.JSONObject) (string, error) {
resource = fmt.Sprintf("%s/%s", resource, action)
resp, err := jsonRequest(self.client, "POST", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, params, body, self.Debug)
resp, err := jsonRequest(self.client, "POST", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, params, body, self.debug)
if err != nil {
return "", err
}
@@ -316,7 +336,7 @@ func (self *SGoogleClient) rdsDo(resource string, action string, params map[stri
}
func (self *SGoogleClient) rdsPatch(resource string, body jsonutils.JSONObject) (string, error) {
resp, err := jsonRequest(self.client, "PATCH", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, nil, body, self.Debug)
resp, err := jsonRequest(self.client, "PATCH", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, nil, body, self.debug)
if err != nil {
return "", err
}
@@ -326,7 +346,7 @@ func (self *SGoogleClient) rdsPatch(resource string, body jsonutils.JSONObject)
func (self *SGoogleClient) rdsUpdate(resource string, params map[string]string, body jsonutils.JSONObject) (string, error) {
resource = fmt.Sprintf("projects/%s/%s", self.projectId, resource)
resp, err := jsonRequest(self.client, "PUT", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, params, body, self.Debug)
resp, err := jsonRequest(self.client, "PUT", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, params, body, self.debug)
if err != nil {
return "", err
}
@@ -352,7 +372,7 @@ func (self *SGoogleClient) ecsInsert(resource string, body jsonutils.JSONObject,
}
}
}
resp, err := jsonRequest(self.client, "POST", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, nil, body, self.Debug)
resp, err := jsonRequest(self.client, "POST", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, nil, body, self.debug)
if err != nil {
return err
}
@@ -363,7 +383,7 @@ func (self *SGoogleClient) ecsInsert(resource string, body jsonutils.JSONObject,
}
func (self *SGoogleClient) storageInsert(resource string, body jsonutils.JSONObject, retval interface{}) error {
resp, err := jsonRequest(self.client, "POST", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, body, self.Debug)
resp, err := jsonRequest(self.client, "POST", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, body, self.debug)
if err != nil {
return err
}
@@ -374,7 +394,7 @@ func (self *SGoogleClient) storageInsert(resource string, body jsonutils.JSONObj
}
func (self *SGoogleClient) storageUpload(resource string, header http.Header, body io.Reader) (*http.Response, error) {
resp, err := rawRequest(self.client, "POST", GOOGLE_STORAGE_UPLOAD_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, header, body, self.Debug)
resp, err := rawRequest(self.client, "POST", GOOGLE_STORAGE_UPLOAD_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, header, body, self.debug)
if err != nil {
return nil, errors.Wrap(err, "rawRequest")
}
@@ -387,7 +407,7 @@ func (self *SGoogleClient) storageUpload(resource string, header http.Header, bo
}
func (self *SGoogleClient) storageUploadPart(resource string, header http.Header, body io.Reader) (*http.Response, error) {
resp, err := rawRequest(self.client, "PUT", GOOGLE_STORAGE_UPLOAD_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, header, body, self.Debug)
resp, err := rawRequest(self.client, "PUT", GOOGLE_STORAGE_UPLOAD_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, header, body, self.debug)
if err != nil {
return nil, errors.Wrap(err, "rawRequest")
}
@@ -400,7 +420,7 @@ func (self *SGoogleClient) storageUploadPart(resource string, header http.Header
}
func (self *SGoogleClient) storageAbortUpload(resource string) error {
resp, err := rawRequest(self.client, "DELETE", GOOGLE_STORAGE_UPLOAD_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, nil, self.Debug)
resp, err := rawRequest(self.client, "DELETE", GOOGLE_STORAGE_UPLOAD_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, nil, self.debug)
if err != nil {
return errors.Wrap(err, "rawRequest")
}
@@ -413,7 +433,7 @@ func (self *SGoogleClient) storageAbortUpload(resource string) error {
}
func (self *SGoogleClient) storageDownload(resource string, header http.Header) (io.ReadCloser, error) {
resp, err := rawRequest(self.client, "GET", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, header, nil, self.Debug)
resp, err := rawRequest(self.client, "GET", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, header, nil, self.debug)
if err != nil {
return nil, errors.Wrap(err, "rawRequest")
}
@@ -426,7 +446,7 @@ func (self *SGoogleClient) storageDownload(resource string, header http.Header)
}
func (self *SGoogleClient) storageList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
return jsonRequest(self.client, "GET", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, params, nil, self.Debug)
return jsonRequest(self.client, "GET", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, params, nil, self.debug)
}
func (self *SGoogleClient) storageListAll(resource string, params map[string]string, retval interface{}) error {
@@ -458,7 +478,7 @@ func (self *SGoogleClient) storageListAll(resource string, params map[string]str
}
func (self *SGoogleClient) storageGet(resource string, retval interface{}) error {
resp, err := jsonRequest(self.client, "GET", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, nil, self.Debug)
resp, err := jsonRequest(self.client, "GET", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, nil, self.debug)
if err != nil {
return err
}
@@ -472,7 +492,7 @@ func (self *SGoogleClient) storageGet(resource string, retval interface{}) error
}
func (self *SGoogleClient) storagePut(resource string, body jsonutils.JSONObject, retval interface{}) error {
resp, err := jsonRequest(self.client, "PUT", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, body, self.Debug)
resp, err := jsonRequest(self.client, "PUT", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, nil, body, self.debug)
if err != nil {
return err
}
@@ -486,7 +506,7 @@ func (self *SGoogleClient) storagePut(resource string, body jsonutils.JSONObject
}
func (self *SGoogleClient) storageDelete(id string, retval interface{}) error {
resp, err := jsonRequest(self.client, "DELETE", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, id, nil, nil, self.Debug)
resp, err := jsonRequest(self.client, "DELETE", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, id, nil, nil, self.debug)
if err != nil {
return err
}
@@ -498,7 +518,7 @@ func (self *SGoogleClient) storageDelete(id string, retval interface{}) error {
func (self *SGoogleClient) storageDo(resource string, action string, params map[string]string, body jsonutils.JSONObject) (string, error) {
resource = fmt.Sprintf("%s/%s", resource, action)
resp, err := jsonRequest(self.client, "POST", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, params, body, self.Debug)
resp, err := jsonRequest(self.client, "POST", GOOGLE_STORAGE_DOMAIN, GOOGLE_STORAGE_API_VERSION, resource, params, body, self.debug)
if err != nil {
return "", err
}
@@ -507,7 +527,7 @@ func (self *SGoogleClient) storageDo(resource string, action string, params map[
}
func (self *SGoogleClient) cloudbuildGet(resource string, retval interface{}) error {
resp, err := jsonRequest(self.client, "GET", GOOGLE_CLOUDBUILD_DOMAIN, GOOGLE_CLOUDBUILD_API_VERSION, resource, nil, nil, self.Debug)
resp, err := jsonRequest(self.client, "GET", GOOGLE_CLOUDBUILD_DOMAIN, GOOGLE_CLOUDBUILD_API_VERSION, resource, nil, nil, self.debug)
if err != nil {
return err
}
@@ -521,7 +541,7 @@ func (self *SGoogleClient) cloudbuildGet(resource string, retval interface{}) er
}
func (self *SGoogleClient) cloudbuildInsert(resource string, body jsonutils.JSONObject, retval interface{}) error {
resp, err := jsonRequest(self.client, "POST", GOOGLE_CLOUDBUILD_DOMAIN, GOOGLE_CLOUDBUILD_API_VERSION, resource, nil, body, self.Debug)
resp, err := jsonRequest(self.client, "POST", GOOGLE_CLOUDBUILD_DOMAIN, GOOGLE_CLOUDBUILD_API_VERSION, resource, nil, body, self.debug)
if err != nil {
return err
}
@@ -533,7 +553,7 @@ func (self *SGoogleClient) cloudbuildInsert(resource string, body jsonutils.JSON
func (self *SGoogleClient) rdsInsert(resource string, body jsonutils.JSONObject, retval interface{}) error {
resource = fmt.Sprintf("projects/%s/%s", self.projectId, resource)
resp, err := jsonRequest(self.client, "POST", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, nil, body, self.Debug)
resp, err := jsonRequest(self.client, "POST", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, nil, body, self.debug)
if err != nil {
return err
}
@@ -544,7 +564,7 @@ func (self *SGoogleClient) rdsInsert(resource string, body jsonutils.JSONObject,
}
func (self *SGoogleClient) rdsGet(resource string, retval interface{}) error {
resp, err := jsonRequest(self.client, "GET", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, nil, nil, self.Debug)
resp, err := jsonRequest(self.client, "GET", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, nil, nil, self.debug)
if err != nil {
return err
}
@@ -559,7 +579,7 @@ func (self *SGoogleClient) rdsGet(resource string, retval interface{}) error {
func (self *SGoogleClient) rdsList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
resource = fmt.Sprintf("projects/%s/%s", self.projectId, resource)
return jsonRequest(self.client, "GET", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, params, nil, self.Debug)
return jsonRequest(self.client, "GET", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION, resource, params, nil, self.debug)
}
func (self *SGoogleClient) rdsListAll(resource string, params map[string]string, retval interface{}) error {
@@ -591,7 +611,7 @@ func (self *SGoogleClient) rdsListAll(resource string, params map[string]string,
}
func (self *SGoogleClient) billingList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
return jsonRequest(self.client, "GET", GOOGLE_BILLING_DOMAIN, GOOGLE_BILLING_API_VERSION, resource, params, nil, self.Debug)
return jsonRequest(self.client, "GET", GOOGLE_BILLING_DOMAIN, GOOGLE_BILLING_API_VERSION, resource, params, nil, self.debug)
}
func (self *SGoogleClient) billingListAll(resource string, params map[string]string, retval interface{}) error {
@@ -623,7 +643,7 @@ func (self *SGoogleClient) billingListAll(resource string, params map[string]str
}
func (self *SGoogleClient) monitorList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
return jsonRequest(self.client, "GET", GOOGLE_MONITOR_DOMAIN, GOOGLE_MONITOR_API_VERSION, resource, params, nil, self.Debug)
return jsonRequest(self.client, "GET", GOOGLE_MONITOR_DOMAIN, GOOGLE_MONITOR_API_VERSION, resource, params, nil, self.debug)
}
func (self *SGoogleClient) monitorListAll(resource string, params map[string]string, retval interface{}) error {
@@ -723,7 +743,7 @@ func (client *SGoogleClient) GetSubAccounts() ([]cloudprovider.SSubAccount, erro
accounts := []cloudprovider.SSubAccount{}
for _, project := range projects {
subAccount := cloudprovider.SSubAccount{}
subAccount.Name = client.providerName
subAccount.Name = client.cpcfg.Name
subAccount.Account = fmt.Sprintf("%s/%s", project.ProjectId, client.clientEmail)
if project.LifecycleState == "ACTIVE" {
subAccount.HealthStatus = api.CLOUD_PROVIDER_HEALTH_NORMAL
+1 -1
View File
@@ -44,7 +44,7 @@ func (host *SHost) GetGlobalId() string {
}
func (host *SHost) GetName() string {
return fmt.Sprintf("%s-%s", host.zone.region.client.providerName, host.zone.GetName())
return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Name, host.zone.GetName())
}
func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
+6 -1
View File
@@ -115,7 +115,12 @@ func (self *SGoogleProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig
return nil, fmt.Errorf("Invalid projectID or client email for google cloud %s", cfg.Account)
}
projectID, clientEmail = accountInfo[0], accountInfo[1]
client, err := google.NewGoogleClient(cfg.Id, cfg.Name, projectID, clientEmail, privateKeyID, privateKey, false)
client, err := google.NewGoogleClient(
google.NewGoogleClientConfig(
projectID, clientEmail, privateKeyID, privateKey,
).CloudproviderConfig(cfg),
)
if err != nil {
return nil, err
}
+3 -3
View File
@@ -46,11 +46,11 @@ func (cache *SStoragecache) GetMetadata() *jsonutils.JSONDict {
}
func (cache *SStoragecache) GetId() string {
return cache.region.client.providerId
return cache.region.client.cpcfg.Id
}
func (cache *SStoragecache) GetName() string {
return cache.region.client.providerName
return cache.region.client.cpcfg.Name
}
func (cache *SStoragecache) GetStatus() string {
@@ -62,7 +62,7 @@ func (cache *SStoragecache) Refresh() error {
}
func (cache *SStoragecache) GetGlobalId() string {
return cache.region.client.providerId
return cache.region.client.cpcfg.Id
}
func (cache *SStoragecache) IsEmulated() bool {