yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/client/modules/mod_dbinstance.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 modules
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/manager"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/responses"
    25  )
    26  
    27  type SDBInstanceManager struct {
    28  	SResourceManager
    29  }
    30  
    31  func NewDBInstanceManager(cfg manager.IManagerConfig) *SDBInstanceManager {
    32  	return &SDBInstanceManager{SResourceManager: SResourceManager{
    33  		SBaseManager:  NewBaseManager(cfg),
    34  		ServiceName:   ServiceNameRDS,
    35  		Region:        cfg.GetRegionId(),
    36  		ProjectId:     cfg.GetProjectId(),
    37  		version:       "v3",
    38  		Keyword:       "",
    39  		KeywordPlural: "instances",
    40  
    41  		ResourceKeyword: "instances",
    42  	}}
    43  }
    44  
    45  func (self *SDBInstanceManager) Get(id string, querys map[string]string) (jsonutils.JSONObject, error) {
    46  	if len(id) == 0 {
    47  		return nil, cloudprovider.ErrNotFound
    48  	}
    49  	resp, err := self.GetInContextWithSpec(nil, "", "", map[string]string{"id": id}, "")
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	instances, err := resp.GetArray("instances")
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	if len(instances) == 0 {
    58  		return nil, cloudprovider.ErrNotFound
    59  	} else if len(instances) == 1 {
    60  		return instances[0], nil
    61  	}
    62  	return nil, cloudprovider.ErrDuplicateId
    63  }
    64  
    65  func (self *SDBInstanceManager) ListParameters(queries map[string]string) (*responses.ListResult, error) {
    66  	id, _ := queries["instance_id"]
    67  	if len(id) == 0 {
    68  		return nil, fmt.Errorf("SDBInstanceManager.ListParameters missing parameter instance_id")
    69  	}
    70  
    71  	delete(queries, "instance_id")
    72  	return self.ListInContextWithSpec(nil, fmt.Sprintf("%s/configurations", id), queries, "configuration_parameters")
    73  }
    74  
    75  func (self *SDBInstanceManager) ListDatabases(queries map[string]string) (*responses.ListResult, error) {
    76  	id, _ := queries["instance_id"]
    77  	if len(id) == 0 {
    78  		return nil, fmt.Errorf("SDBInstanceManager.ListDatabases missing parameter instance_id")
    79  	}
    80  
    81  	delete(queries, "instance_id")
    82  	return self.ListInContextWithSpec(nil, fmt.Sprintf("%s/database/detail", id), queries, "databases")
    83  }
    84  
    85  func (self *SDBInstanceManager) ListAccounts(queries map[string]string) (*responses.ListResult, error) {
    86  	id, _ := queries["instance_id"]
    87  	if len(id) == 0 {
    88  		return nil, fmt.Errorf("SDBInstanceManager.ListAccounts missing parameter instance_id")
    89  	}
    90  
    91  	delete(queries, "instance_id")
    92  	return self.ListInContextWithSpec(nil, fmt.Sprintf("%s/db_user/detail", id), queries, "users")
    93  }
    94  
    95  func (self *SDBInstanceManager) ListPrivileges(queries map[string]string) (*responses.ListResult, error) {
    96  	id, _ := queries["instance_id"]
    97  	if len(id) == 0 {
    98  		return nil, fmt.Errorf("SDBInstanceManager.ListPrivileges missing parameter instance_id")
    99  	}
   100  
   101  	delete(queries, "instance_id")
   102  	return self.ListInContextWithSpec(nil, fmt.Sprintf("%s/db_user/database", id), queries, "databases")
   103  }