Last active
May 21, 2022 15:33
-
-
Save eumel8/19b37942feebcc19b7a4c68a35b1ab7a to your computer and use it in GitHub Desktop.
otc-rds-nextflavor.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// show the next available rds flavor on OTC | |
package main | |
import ( | |
"fmt" | |
"sort" | |
"strconv" | |
"strings" | |
"net/http" | |
"os" | |
"github.com/gophercloud/utils/client" | |
golangsdk "github.com/opentelekomcloud/gophertelekomcloud" | |
"github.com/opentelekomcloud/gophertelekomcloud/openstack" | |
"github.com/opentelekomcloud/gophertelekomcloud/openstack/rds/v3/flavors" | |
"k8s.io/klog/v2" | |
) | |
type posflavor []struct { | |
VCPUs int | |
RAM int | |
Spec string | |
} | |
func rdsFlavorLookup(client *golangsdk.ServiceClient, availabilityzone string, datastoretype string, datastoreversion string, flavorref string, raisetype string) (string, error) { | |
azs := strings.Split(availabilityzone, ",") | |
var ( | |
az1 string | |
az2 string | |
) | |
az1 = string(azs[0]) | |
if len(azs) > 1 { | |
az2 = string(azs[1]) | |
} else { | |
az2 = "" | |
} | |
curCpu := "0" | |
curMem := 0 | |
posflavor := posflavor{} | |
listOpts := flavors.ListOpts{ | |
VersionName: datastoreversion, | |
} | |
allFlavorPages, err := flavors.List(client, listOpts, datastoretype).AllPages() | |
if err != nil { | |
klog.Exitf("unable to list flavor: %v", err) | |
return "", err | |
} | |
rdsFlavors, err := flavors.ExtractDbFlavors(allFlavorPages) | |
if err != nil { | |
klog.Exitf("unable to extract flavor: %v", err) | |
return "", err | |
} | |
for _, rds := range rdsFlavors { | |
if rds.SpecCode == flavorref { | |
curCpu = rds.VCPUs | |
curMem = rds.RAM | |
fmt.Printf("hillers code: %s\t%d\t%s\n", rds.VCPUs, rds.RAM, rds.SpecCode) | |
} | |
} | |
switch raisetype { | |
case "cpu": | |
for _, rds := range rdsFlavors { | |
for n, az := range rds.AzStatus { | |
if n == az1 && az == "normal" { | |
for l, az := range rds.AzStatus { | |
if az2 == "" || l == az2 && az == "normal" { | |
if strings.HasSuffix(flavorref, ".ha") && strings.HasSuffix(rds.SpecCode, ".ha") && rds.VCPUs > curCpu { | |
iCpu, _ := strconv.Atoi(rds.VCPUs) | |
posflavor = append(posflavor, struct { | |
VCPUs int | |
RAM int | |
Spec string | |
}{iCpu, rds.RAM, rds.SpecCode}) | |
} | |
if !strings.HasSuffix(flavorref, ".ha") && !strings.HasSuffix(rds.SpecCode, ".rr") && !strings.HasSuffix(rds.SpecCode, ".ha") { | |
iCpu, _ := strconv.Atoi(rds.VCPUs) | |
posflavor = append(posflavor, struct { | |
VCPUs int | |
RAM int | |
Spec string | |
}{iCpu, rds.RAM, rds.SpecCode}) | |
} | |
} | |
} | |
} | |
} | |
} | |
sort.Slice(posflavor, func(i, j int) bool { | |
return posflavor[i].VCPUs < posflavor[j].VCPUs | |
}) | |
if len(posflavor) > 0 { | |
return posflavor[0].Spec, nil | |
} | |
case "mem": | |
for _, rds := range rdsFlavors { | |
for n, az := range rds.AzStatus { | |
if n == az1 && az == "normal" { | |
for l, az := range rds.AzStatus { | |
if l == az2 && az == "normal" { | |
if strings.HasSuffix(flavorref, ".ha") && strings.HasSuffix(rds.SpecCode, ".ha") && rds.RAM > curMem { | |
iCpu, _ := strconv.Atoi(rds.VCPUs) | |
posflavor = append(posflavor, struct { | |
VCPUs int | |
RAM int | |
Spec string | |
}{iCpu, rds.RAM, rds.SpecCode}) | |
} | |
if !strings.HasSuffix(flavorref, ".ha") && !strings.HasSuffix(rds.SpecCode, ".rr") && !strings.HasSuffix(rds.SpecCode, ".ha") && rds.RAM > curMem { | |
iCpu, _ := strconv.Atoi(rds.VCPUs) | |
posflavor = append(posflavor, struct { | |
VCPUs int | |
RAM int | |
Spec string | |
}{iCpu, rds.RAM, rds.SpecCode}) | |
} | |
} | |
} | |
} | |
} | |
} | |
sort.SliceStable(posflavor, func(i, j int) bool { | |
return posflavor[i].RAM < posflavor[j].RAM | |
}) | |
if len(posflavor) > 0 { | |
return posflavor[0].Spec, nil | |
} | |
} | |
return "", fmt.Errorf("no flavor found") | |
} | |
func getProvider() *golangsdk.ProviderClient { | |
if os.Getenv("OS_AUTH_URL") == "" { | |
os.Setenv("OS_AUTH_URL", "https://iam.eu-de.otc.t-systems.com:443/v3") | |
} | |
if os.Getenv("OS_IDENTITY_API_VERSION") == "" { | |
os.Setenv("OS_IDENTITY_API_VERSION", "3") | |
} | |
if os.Getenv("OS_REGION_NAME") == "" { | |
os.Setenv("OS_REGION_NAME", "eu-de") | |
} | |
if os.Getenv("OS_PROJECT_NAME") == "" { | |
os.Setenv("OS_PROJECT_NAME", "eu-de") | |
} | |
opts, err := openstack.AuthOptionsFromEnv() | |
if err != nil { | |
klog.Exitf("error getting auth from env: %v", err) | |
} | |
provider, err := openstack.AuthenticatedClient(opts) | |
if err != nil { | |
klog.Exitf("unable to initialize openstack client: %v", err) | |
} | |
if os.Getenv("OS_DEBUG") != "" { | |
provider.HTTPClient = http.Client{ | |
Transport: &client.RoundTripper{ | |
Rt: &http.Transport{}, | |
Logger: &client.DefaultLogger{}, | |
}, | |
} | |
} | |
return provider | |
} | |
func main() { | |
provider := getProvider() | |
rds, err := openstack.NewRDSV3(provider, golangsdk.EndpointOpts{}) | |
if err != nil { | |
klog.Exitf("unable to initialize rds client: %v", err) | |
return | |
} | |
availabilityzone := "eu-de-01" | |
//availabilityzone := "eu-de-01,eu-de-02" | |
datastoretype := "MySQL" | |
datastoreversion := "8.0" | |
flavorref := "rds.mysql.m1.medium.ha" | |
raisetype := "cpu" | |
newflavor, err := rdsFlavorLookup(rds, availabilityzone, datastoretype, datastoreversion, flavorref, raisetype) | |
if err != nil { | |
klog.Exitf("rds flavor lookup failed: %v", err) | |
} | |
fmt.Println("New flavor: ", newflavor) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment