github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cce/v3/nodepools/requests.go (about) 1 package nodepools 2 3 import ( 4 "reflect" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes" 8 ) 9 10 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 11 MoreHeaders: map[string]string{"Content-Type": "application/json"}, 12 } 13 14 // ListOpts allows the filtering of list data using given parameters. 15 type ListOpts struct { 16 Name string `json:"name"` 17 Uid string `json:"uid"` 18 Phase string `json:"phase"` 19 } 20 21 // List returns collection of node pools. 22 func List(client *golangsdk.ServiceClient, clusterID string, opts ListOpts) ([]NodePool, error) { 23 var r ListResult 24 _, r.Err = client.Get(rootURL(client, clusterID), &r.Body, &golangsdk.RequestOpts{ 25 OkCodes: []int{200}, 26 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 27 }) 28 29 allNodePools, err := r.ExtractNodePool() 30 31 if err != nil { 32 return nil, err 33 } 34 35 return FilterNodePools(allNodePools, opts), nil 36 } 37 38 func FilterNodePools(nodepools []NodePool, opts ListOpts) []NodePool { 39 40 var refinedNodePools []NodePool 41 var matched bool 42 43 m := map[string]FilterStruct{} 44 45 if opts.Name != "" { 46 m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}} 47 } 48 if opts.Uid != "" { 49 m["Id"] = FilterStruct{Value: opts.Uid, Driller: []string{"Metadata"}} 50 } 51 52 if opts.Phase != "" { 53 m["Phase"] = FilterStruct{Value: opts.Phase, Driller: []string{"Status"}} 54 } 55 56 if len(m) > 0 && len(nodepools) > 0 { 57 for _, nodepool := range nodepools { 58 matched = true 59 60 for key, value := range m { 61 if sVal := GetStructNestedField(&nodepool, key, value.Driller); !(sVal == value.Value) { 62 matched = false 63 } 64 } 65 if matched { 66 refinedNodePools = append(refinedNodePools, nodepool) 67 } 68 } 69 } else { 70 refinedNodePools = nodepools 71 } 72 return refinedNodePools 73 } 74 75 func GetStructNestedField(v *NodePool, field string, structDriller []string) string { 76 r := reflect.ValueOf(v) 77 for _, drillField := range structDriller { 78 f := reflect.Indirect(r).FieldByName(drillField).Interface() 79 r = reflect.ValueOf(f) 80 } 81 f1 := reflect.Indirect(r).FieldByName(field) 82 return string(f1.String()) 83 } 84 85 type FilterStruct struct { 86 Value string 87 Driller []string 88 } 89 90 // CreateOpts allows extensions to add additional parameters to the 91 // Create request. 92 type CreateOpts struct { 93 // API type, fixed value Node 94 Kind string `json:"kind" required:"true"` 95 // API version, fixed value v3 96 ApiVersion string `json:"apiversion" required:"true"` 97 // Metadata required to create a Node Pool 98 Metadata CreateMetaData `json:"metadata"` 99 // specifications to create a Node Pool 100 Spec CreateSpec `json:"spec" required:"true"` 101 } 102 103 // CreateMetaData required to create a Node Pool 104 type CreateMetaData struct { 105 // Name of the node pool. 106 Name string `json:"name" required:"true"` 107 } 108 109 // CreateSpec describes Node pools specification 110 type CreateSpec struct { 111 //Node pool type 112 Type string `json:"type,omitempty"` 113 // Node template 114 NodeTemplate nodes.Spec `json:"nodeTemplate" required:"true"` 115 // Initial number of expected nodes 116 InitialNodeCount *int `json:"initialNodeCount" required:"true"` 117 // Auto scaling parameters 118 Autoscaling AutoscalingSpec `json:"autoscaling"` 119 // Node management parameters 120 NodeManagement NodeManagementSpec `json:"nodeManagement"` 121 } 122 123 // Create accepts a CreateOpts struct and uses the values to create a new 124 // logical Node Pool. When it is created, the Node Pool does not have an internal 125 // interface 126 type CreateOptsBuilder interface { 127 ToNodePoolCreateMap() (map[string]interface{}, error) 128 } 129 130 // ToNodePoolCreateMap builds a create request body from CreateOpts. 131 func (opts CreateOpts) ToNodePoolCreateMap() (map[string]interface{}, error) { 132 return golangsdk.BuildRequestBody(opts, "") 133 } 134 135 // Create accepts a CreateOpts struct and uses the values to create a new 136 // logical node pool. 137 func Create(c *golangsdk.ServiceClient, clusterid string, opts CreateOptsBuilder) (r CreateResult) { 138 b, err := opts.ToNodePoolCreateMap() 139 if err != nil { 140 r.Err = err 141 return 142 } 143 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}} 144 _, r.Err = c.Post(rootURL(c, clusterid), b, &r.Body, reqOpt) 145 return 146 } 147 148 // Get retrieves a particular node pool based on its unique ID and cluster ID. 149 func Get(c *golangsdk.ServiceClient, clusterid, nodepoolid string) (r GetResult) { 150 _, r.Err = c.Get(resourceURL(c, clusterid, nodepoolid), &r.Body, &golangsdk.RequestOpts{ 151 OkCodes: []int{200}, 152 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 153 }) 154 return 155 } 156 157 // UpdateOptsBuilder allows extensions to add additional parameters to the 158 // Update request. 159 type UpdateOptsBuilder interface { 160 ToNodePoolUpdateMap() (map[string]interface{}, error) 161 } 162 163 // UpdateOpts contains all the values needed to update a new node pool 164 type UpdateOpts struct { 165 // API type, fixed value Node 166 Kind string `json:"kind" required:"true"` 167 // API version, fixed value v3 168 ApiVersion string `json:"apiversion" required:"true"` 169 // Metadata required to update a Node Pool 170 Metadata UpdateMetaData `json:"metadata" required:"true"` 171 // specifications to update a Node Pool 172 Spec UpdateSpec `json:"spec,omitempty" required:"true"` 173 } 174 175 // UpdateMetaData required to update a Node Pool 176 type UpdateMetaData struct { 177 // Name of the node pool. 178 Name string `json:"name" required:"true"` 179 } 180 181 // UpdateSpec describes Node pools update specification 182 type UpdateSpec struct { 183 // Node type. Currently, only VM nodes are supported. 184 Type string `json:"type,omitempty"` 185 // Node template 186 NodeTemplate nodes.Spec `json:"nodeTemplate"` 187 // Initial number of expected nodes 188 InitialNodeCount *int `json:"initialNodeCount" required:"true"` 189 // Auto scaling parameters 190 Autoscaling AutoscalingSpec `json:"autoscaling"` 191 } 192 193 // ToNodePoolUpdateMap builds an update body based on UpdateOpts. 194 func (opts UpdateOpts) ToNodePoolUpdateMap() (map[string]interface{}, error) { 195 return golangsdk.BuildRequestBody(opts, "") 196 } 197 198 // Update allows node pools to be updated. 199 func Update(c *golangsdk.ServiceClient, clusterid, nodepoolid string, opts UpdateOptsBuilder) (r UpdateResult) { 200 b, err := opts.ToNodePoolUpdateMap() 201 if err != nil { 202 r.Err = err 203 return 204 } 205 _, r.Err = c.Put(resourceURL(c, clusterid, nodepoolid), b, &r.Body, &golangsdk.RequestOpts{ 206 OkCodes: []int{200}, 207 }) 208 return 209 } 210 211 // Delete will permanently delete a particular node pool based on its unique ID and cluster ID. 212 func Delete(c *golangsdk.ServiceClient, clusterid, nodepoolid string) (r DeleteResult) { 213 _, r.Err = c.Delete(resourceURL(c, clusterid, nodepoolid), &golangsdk.RequestOpts{ 214 OkCodes: []int{200}, 215 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 216 }) 217 return 218 }