yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/rds_sqlserver_database.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  	"time"
    21  
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SSQLServerDatabase struct {
    30  	multicloud.SDBInstanceDatabaseBase
    31  	AzureTags
    32  	rds *SSQLServer
    33  	Sku struct {
    34  		Name     string `json:"name"`
    35  		Tier     string `json:"tier"`
    36  		Capacity int    `json:"capacity"`
    37  	} `json:"sku"`
    38  	Kind       string `json:"kind"`
    39  	Properties struct {
    40  		Collation                     string    `json:"collation"`
    41  		Maxsizebytes                  int64     `json:"maxSizeBytes"`
    42  		Status                        string    `json:"status"`
    43  		Databaseid                    string    `json:"databaseId"`
    44  		Creationdate                  string    `json:"creationDate"`
    45  		Currentserviceobjectivename   string    `json:"currentServiceObjectiveName"`
    46  		Requestedserviceobjectivename string    `json:"requestedServiceObjectiveName"`
    47  		Defaultsecondarylocation      string    `json:"defaultSecondaryLocation"`
    48  		Catalogcollation              string    `json:"catalogCollation"`
    49  		Licensetype                   string    `json:"licenseType"`
    50  		Maxlogsizebytes               int       `json:"maxLogSizeBytes"`
    51  		Storageaccounttype            string    `json:"storageAccountType"`
    52  		Zoneredundant                 bool      `json:"zoneRedundant"`
    53  		Readscale                     string    `json:"readScale"`
    54  		Earliestrestoredate           time.Time `json:"earliestRestoreDate"`
    55  		Currentsku                    struct {
    56  			Name     string `json:"name"`
    57  			Tier     string `json:"tier"`
    58  			Capacity int    `json:"capacity"`
    59  			Family   string `json:"family"`
    60  		} `json:"currentSku"`
    61  	} `json:"properties"`
    62  	Location string `json:"location"`
    63  	ID       string `json:"id"`
    64  	Name     string `json:"name"`
    65  	Type     string `json:"type"`
    66  }
    67  
    68  func (self *SSQLServerDatabase) GetName() string {
    69  	return self.Name
    70  }
    71  
    72  func (self *SSQLServerDatabase) GetId() string {
    73  	return self.ID
    74  }
    75  
    76  func (self *SSQLServerDatabase) GetStatus() string {
    77  	switch self.Properties.Status {
    78  	case "Online":
    79  		return api.DBINSTANCE_DATABASE_RUNNING
    80  	case "Creating":
    81  		return api.DBINSTANCE_DATABASE_CREATING
    82  	default:
    83  		return strings.ToLower(self.Properties.Status)
    84  	}
    85  }
    86  
    87  func (self *SSQLServerDatabase) GetDiskSizeMb() int {
    88  	return int(self.Properties.Maxsizebytes / 1024 / 1024)
    89  }
    90  
    91  func (self *SSQLServerDatabase) GetVcpuCount() int {
    92  	if len(self.Properties.Currentsku.Family) > 0 {
    93  		return self.Properties.Currentsku.Capacity
    94  	}
    95  	return 0
    96  }
    97  
    98  func (self *SSQLServerDatabase) GetVmemSizeMb() int {
    99  	if len(self.Properties.Currentsku.Family) > 0 {
   100  		return int(5.2 * 1024 * float32(self.Properties.Currentsku.Capacity))
   101  	}
   102  	return 0
   103  }
   104  
   105  func (self *SSQLServerDatabase) GetDTU() int {
   106  	if len(self.Properties.Currentsku.Family) == 0 {
   107  		return self.Properties.Currentsku.Capacity
   108  	}
   109  	return 0
   110  }
   111  
   112  func (self *SSQLServerDatabase) GetGlobalId() string {
   113  	return strings.ToLower(self.Name)
   114  }
   115  
   116  func (self *SSQLServerDatabase) GetCharacterSet() string {
   117  	return self.Properties.Collation
   118  }
   119  
   120  func (self *SRegion) GetSQLServerDatabases(id string) ([]SSQLServerDatabase, error) {
   121  	result := struct {
   122  		Value []SSQLServerDatabase
   123  	}{}
   124  	return result.Value, self.get(id+"/databases", url.Values{}, &result)
   125  }
   126  
   127  func (self *SSQLServer) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) {
   128  	dbs, err := self.fetchDatabase()
   129  	if err != nil {
   130  		return nil, errors.Wrapf(err, "fetchDatabase")
   131  	}
   132  	ret := []cloudprovider.ICloudDBInstanceDatabase{}
   133  	for i := range dbs {
   134  		dbs[i].rds = self
   135  		ret = append(ret, &dbs[i])
   136  	}
   137  	return ret, nil
   138  }