yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/rds_managed_sqlserver.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 azure
    16  
    17  import (
    18  	"net/url"
    19  	"strings"
    20  
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SManagedSQLServer struct {
    28  	region *SRegion
    29  	multicloud.SDBInstanceBase
    30  	AzureTags
    31  
    32  	Location string `json:"location"`
    33  	ID       string `json:"id"`
    34  	Name     string `json:"name"`
    35  	Type     string `json:"type"`
    36  	Sku      struct {
    37  		Name     string `json:"name"`
    38  		Tier     string `json:"tier"`
    39  		Capacity int    `json:"capacity"`
    40  		Family   string `json:"family"`
    41  	} `json:"sku"`
    42  	Properties struct {
    43  		Fullyqualifieddomainname   string `json:"fullyQualifiedDomainName"`
    44  		Administratorlogin         string `json:"administratorLogin"`
    45  		Subnetid                   string `json:"subnetId"`
    46  		State                      string `json:"state"`
    47  		Provisioningstate          string `json:"provisioningState"`
    48  		Vcores                     int    `json:"vCores"`
    49  		Storagesizeingb            int    `json:"storageSizeInGB"`
    50  		Licensetype                string `json:"licenseType"`
    51  		Collation                  string `json:"collation"`
    52  		Publicdataendpointenabled  bool   `json:"publicDataEndpointEnabled"`
    53  		Proxyoverride              string `json:"proxyOverride"`
    54  		Minimaltlsversion          string `json:"minimalTlsVersion"`
    55  		Dnszone                    string `json:"dnsZone"`
    56  		Maintenanceconfigurationid string `json:"maintenanceConfigurationId"`
    57  		Storageaccounttype         string `json:"storageAccountType"`
    58  	} `json:"properties"`
    59  }
    60  
    61  func (self *SRegion) ListManagedSQLServer() ([]SManagedSQLServer, error) {
    62  	rds := []SManagedSQLServer{}
    63  	err := self.list("Microsoft.Sql/managedInstances", url.Values{}, &rds)
    64  	if err != nil {
    65  		return nil, errors.Wrapf(err, "list")
    66  	}
    67  	return rds, nil
    68  }
    69  
    70  func (self *SRegion) GetManagedSQLServer(id string) (*SManagedSQLServer, error) {
    71  	rds := &SManagedSQLServer{region: self}
    72  	return rds, self.get(id, url.Values{}, rds)
    73  }
    74  
    75  func (self *SManagedSQLServer) GetDiskSizeGB() int {
    76  	return self.Properties.Storagesizeingb
    77  }
    78  
    79  func (self *SManagedSQLServer) GetEngine() string {
    80  	return api.DBINSTANCE_TYPE_SQLSERVER
    81  }
    82  
    83  func (self *SManagedSQLServer) GetEngineVersion() string {
    84  	return "latest"
    85  }
    86  
    87  func (self *SManagedSQLServer) GetGlobalId() string {
    88  	return strings.ToLower(self.ID)
    89  }
    90  
    91  func (self *SManagedSQLServer) GetCategory() string {
    92  	return self.Sku.Family
    93  }
    94  
    95  func (self *SManagedSQLServer) GetProjectId() string {
    96  	return getResourceGroup(self.ID)
    97  }
    98  
    99  func (self *SManagedSQLServer) GetIVpcId() string {
   100  	if len(self.Properties.Subnetid) > 0 {
   101  		info := strings.Split(self.Properties.Subnetid, "/")
   102  		if len(info) > 2 {
   103  			return strings.Join(info[:len(info)-2], "/")
   104  		}
   105  	}
   106  	return ""
   107  }
   108  
   109  func (self *SManagedSQLServer) GetId() string {
   110  	return self.ID
   111  }
   112  
   113  func (self *SManagedSQLServer) GetInstanceType() string {
   114  	return self.Sku.Name
   115  }
   116  
   117  func (self *SManagedSQLServer) GetMaintainTime() string {
   118  	return ""
   119  }
   120  
   121  func (self *SManagedSQLServer) GetName() string {
   122  	return self.Name
   123  }
   124  
   125  func (self *SManagedSQLServer) GetPort() int {
   126  	return 1433
   127  }
   128  
   129  func (self *SManagedSQLServer) GetStatus() string {
   130  	switch self.Properties.State {
   131  	case "Succeeded", "Running", "Ready":
   132  		return api.DBINSTANCE_RUNNING
   133  	case "Creating", "Created":
   134  		return api.DBINSTANCE_DEPLOYING
   135  	case "Deleted", "Deleting":
   136  		return api.DBINSTANCE_BACKUP_DELETING
   137  	case "Failed":
   138  		return api.DBINSTANCE_CREATE_FAILED
   139  	default:
   140  		return strings.ToLower(self.Properties.State)
   141  	}
   142  }
   143  
   144  func (self *SManagedSQLServer) GetStorageType() string {
   145  	return self.Properties.Storageaccounttype
   146  }
   147  
   148  func (self *SManagedSQLServer) GetVcpuCount() int {
   149  	return self.Properties.Vcores
   150  }
   151  
   152  func (self *SManagedSQLServer) GetVmemSizeMB() int {
   153  	switch self.Sku.Family {
   154  	case "Gen5":
   155  		return int(float32(self.Properties.Vcores) * 5.1 * 1024)
   156  	case "Gen4":
   157  		return self.Properties.Vcores * 7 * 1024
   158  	}
   159  	return 0
   160  }
   161  
   162  func (self *SManagedSQLServer) GetZone1Id() string {
   163  	return ""
   164  }
   165  
   166  func (self *SManagedSQLServer) GetZone2Id() string {
   167  	return ""
   168  }
   169  
   170  func (self *SManagedSQLServer) GetZone3Id() string {
   171  	return ""
   172  }
   173  
   174  func (self *SManagedSQLServer) GetConnectionStr() string {
   175  	return self.Properties.Fullyqualifieddomainname
   176  }