yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/rds_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  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SPrivateendpointconnection struct {
    29  	ID         string `json:"id"`
    30  	Properties struct {
    31  		Provisioningstate string `json:"provisioningState"`
    32  		Privateendpoint   struct {
    33  			ID string `json:"id"`
    34  		} `json:"privateEndpoint"`
    35  		Privatelinkserviceconnectionstate struct {
    36  			Status          string `json:"status"`
    37  			Description     string `json:"description"`
    38  			Actionsrequired string `json:"actionsRequired"`
    39  		} `json:"privateLinkServiceConnectionState"`
    40  	} `json:"properties"`
    41  }
    42  
    43  type SSQLServer struct {
    44  	region *SRegion
    45  	multicloud.SDBInstanceBase
    46  	AzureTags
    47  
    48  	dbs []SSQLServerDatabase
    49  
    50  	Kind       string `json:"kind"`
    51  	Properties struct {
    52  		Administratorlogin         string                       `json:"administratorLogin"`
    53  		Version                    string                       `json:"version"`
    54  		State                      string                       `json:"state"`
    55  		Fullyqualifieddomainname   string                       `json:"fullyQualifiedDomainName"`
    56  		Privateendpointconnections []SPrivateendpointconnection `json:"privateEndpointConnections"`
    57  		Publicnetworkaccess        string                       `json:"publicNetworkAccess"`
    58  	} `json:"properties"`
    59  	Location string `json:"location"`
    60  	ID       string `json:"id"`
    61  	Name     string `json:"name"`
    62  	Type     string `json:"type"`
    63  }
    64  
    65  func (self *SRegion) ListSQLServer() ([]SSQLServer, error) {
    66  	rds := []SSQLServer{}
    67  	err := self.list("Microsoft.Sql/servers", url.Values{}, &rds)
    68  	if err != nil {
    69  		return nil, errors.Wrapf(err, "list")
    70  	}
    71  	return rds, nil
    72  }
    73  
    74  func (self *SRegion) GetSQLServer(id string) (*SSQLServer, error) {
    75  	rds := &SSQLServer{region: self}
    76  	return rds, self.get(id, url.Values{}, rds)
    77  }
    78  
    79  func (self *SSQLServer) GetDiskSizeGB() int {
    80  	dbs, err := self.fetchDatabase()
    81  	if err != nil {
    82  		return 0
    83  	}
    84  	sizeMb := 0
    85  	for _, db := range dbs {
    86  		sizeMb += db.GetDiskSizeMb()
    87  	}
    88  	return sizeMb / 1024
    89  }
    90  
    91  func (self *SSQLServer) GetEngine() string {
    92  	return api.DBINSTANCE_TYPE_SQLSERVER
    93  }
    94  
    95  func (self *SSQLServer) GetEngineVersion() string {
    96  	return self.Properties.Version
    97  }
    98  
    99  func (self *SSQLServer) GetGlobalId() string {
   100  	return strings.ToLower(self.ID)
   101  }
   102  
   103  func (self *SSQLServer) GetProjectId() string {
   104  	return getResourceGroup(self.ID)
   105  }
   106  
   107  func (self *SSQLServer) GetCategory() string {
   108  	return api.AZURE_DBINSTANCE_CATEGORY_BASIC
   109  }
   110  
   111  func (self *SSQLServer) GetIVpcId() string {
   112  	return ""
   113  }
   114  
   115  func (self *SSQLServer) GetId() string {
   116  	return self.ID
   117  }
   118  
   119  func (self *SSQLServer) GetInstanceType() string {
   120  	return ""
   121  }
   122  
   123  func (self *SSQLServer) GetMaintainTime() string {
   124  	return ""
   125  }
   126  
   127  func (self *SSQLServer) GetName() string {
   128  	return self.Name
   129  }
   130  
   131  func (self *SSQLServer) GetPort() int {
   132  	return 1433
   133  }
   134  
   135  func (self *SSQLServer) GetStatus() string {
   136  	switch self.Properties.State {
   137  	case "Ready":
   138  		return api.DBINSTANCE_RUNNING
   139  	default:
   140  		return self.Properties.State
   141  	}
   142  }
   143  
   144  func (self *SSQLServer) GetStorageType() string {
   145  	return api.AZURE_DBINSTANCE_STORAGE_TYPE_DEFAULT
   146  }
   147  
   148  func (self *SSQLServer) fetchDatabase() ([]SSQLServerDatabase, error) {
   149  	if len(self.dbs) > 0 {
   150  		return self.dbs, nil
   151  	}
   152  	var err error
   153  	self.dbs, err = self.region.GetSQLServerDatabases(self.ID)
   154  	return self.dbs, err
   155  }
   156  
   157  func (self *SSQLServer) GetVcpuCount() int {
   158  	dbs, err := self.fetchDatabase()
   159  	if err != nil {
   160  		return 0
   161  	}
   162  	vcpu := 0
   163  	for _, db := range dbs {
   164  		vcpu += db.GetVcpuCount()
   165  	}
   166  	return vcpu
   167  }
   168  
   169  func (self *SSQLServer) GetVmemSizeMB() int {
   170  	dbs, err := self.fetchDatabase()
   171  	if err != nil {
   172  		return 0
   173  	}
   174  	mem := 0
   175  	for _, db := range dbs {
   176  		mem += db.GetVmemSizeMb()
   177  	}
   178  	return mem
   179  }
   180  
   181  func (self *SSQLServer) GetSysTags() map[string]string {
   182  	dtu := self.GetDTU()
   183  	if dtu > 0 {
   184  		return map[string]string{"DTU": fmt.Sprintf("%d", dtu)}
   185  	}
   186  	return nil
   187  }
   188  
   189  func (self *SSQLServer) GetDTU() int {
   190  	dbs, err := self.fetchDatabase()
   191  	if err != nil {
   192  		return 0
   193  	}
   194  	dtu := 0
   195  	for _, db := range dbs {
   196  		dtu += db.GetDTU()
   197  	}
   198  	return dtu
   199  }
   200  
   201  func (self *SSQLServer) GetZone1Id() string {
   202  	return ""
   203  }
   204  
   205  func (self *SSQLServer) GetZone2Id() string {
   206  	return ""
   207  }
   208  
   209  func (self *SSQLServer) GetZone3Id() string {
   210  	return ""
   211  }
   212  
   213  func (self *SSQLServer) GetConnectionStr() string {
   214  	return self.Properties.Fullyqualifieddomainname
   215  }