github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cloud/iks/datacenters.go (about) 1 package iks 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/IBM-Cloud/bluemix-go/client" 8 ) 9 10 type MachineType struct { 11 Name string 12 Memory string 13 NetworkSpeed string 14 Cores string 15 Os string 16 ServerType string 17 Storage string 18 SecondaryStorage string 19 SecondaryStorageEncrypted bool 20 Deprecated bool 21 CorrespondingMachineType string 22 IsTrusted bool 23 Gpus string 24 } 25 26 type VLAN struct { 27 ID string `json:"id"` 28 Type string `json:"type"` 29 Properties struct { 30 Name string `json:"name"` 31 Note string `json:"note"` 32 PrimaryRouter string `json:"primary_router"` 33 VlanNumber string `json:"vlan_number"` 34 VlanType string `json:"vlan_type"` 35 Location string `json:"location"` 36 LocalDiskStorageCapability string `json:"local_disk_storage_capability"` 37 SanStorageCapability string `json:"san_storage_capability"` 38 } `json:"properties"` 39 } 40 41 type MachineTypes interface { 42 GetMachineTypes(zone Zone, region Region) ([]MachineType, error) 43 GetMachineType(machinetypearg string, zone Zone, region Region) (*MachineType, error) 44 } 45 46 type VLANs interface { 47 GetVLANs(zone Zone, region Region) ([]VLAN, error) 48 GetVLAN(vlanarg string, zone Zone, region Region) (*VLAN, error) 49 } 50 51 type machineTypes struct { 52 *client.Client 53 machineTypes map[string][]MachineType 54 } 55 56 type vLANs struct { 57 *client.Client 58 VLANs map[string][]VLAN 59 } 60 61 func newMachineTypesAPI(c *client.Client) MachineTypes { 62 return &machineTypes{ 63 Client: c, 64 machineTypes: nil, 65 } 66 } 67 68 func newVLANsAPI(c *client.Client) VLANs { 69 return &vLANs{ 70 Client: c, 71 VLANs: nil, 72 } 73 } 74 75 func (v *machineTypes) fetch(zone Zone, region Region) error { 76 if v.machineTypes == nil { 77 v.machineTypes = make(map[string][]MachineType) 78 } 79 if _, ok := v.machineTypes[zone.ID]; !ok { 80 machineTypes := []MachineType{} 81 headers := make(map[string]string, 2) 82 headers["datacenter"] = zone.ID 83 headers["X-Region"] = region.Name 84 _, err := v.Client.Get("/v1/datacenters/"+zone.ID+"/machine-types", &machineTypes, headers) 85 if err != nil { 86 return err 87 } 88 v.machineTypes[zone.ID] = machineTypes 89 } 90 return nil 91 } 92 93 func (v *machineTypes) GetMachineTypes(zone Zone, region Region) ([]MachineType, error) { 94 if err := v.fetch(zone, region); err != nil { 95 return nil, err 96 } 97 return v.machineTypes[zone.ID], nil 98 } 99 100 func (v *machineTypes) GetMachineType(machinetypearg string, zone Zone, region Region) (*MachineType, error) { 101 if err := v.fetch(zone, region); err != nil { 102 return nil, err 103 } 104 105 for _, machineType := range v.machineTypes[zone.ID] { 106 if strings.Compare(machinetypearg, machineType.Name) == 0 { 107 return &machineType, nil 108 } 109 } 110 return nil, fmt.Errorf("no machine type %q not found in zone %q", machinetypearg, zone.ID) 111 } 112 113 func (v *vLANs) fetch(zone Zone, region Region) error { 114 if v.VLANs == nil { 115 v.VLANs = make(map[string][]VLAN) 116 } 117 if _, ok := v.VLANs[zone.ID]; !ok { 118 vLANs := []VLAN{} 119 headers := make(map[string]string, 2) 120 headers["datacenter"] = zone.ID 121 headers["X-Region"] = region.Name 122 _, err := v.Client.Get("/v1/datacenters/"+zone.ID+"/vlans", &vLANs, headers) 123 if err != nil { 124 return err 125 } 126 v.VLANs[zone.ID] = vLANs 127 } 128 return nil 129 } 130 131 func (v *vLANs) GetVLANs(zone Zone, region Region) ([]VLAN, error) { 132 if err := v.fetch(zone, region); err != nil { 133 return nil, err 134 } 135 return v.VLANs[zone.ID], nil 136 } 137 138 func (v *vLANs) GetVLAN(vlanarg string, zone Zone, region Region) (*VLAN, error) { 139 if err := v.fetch(zone, region); err != nil { 140 return nil, err 141 } 142 143 for _, vLAN := range v.VLANs[zone.ID] { 144 if strings.Compare(vlanarg, vLAN.ID) == 0 { 145 return &vLAN, nil 146 } 147 } 148 return nil, fmt.Errorf("no machine type %q not found in zone %q", vlanarg, zone.ID) 149 }