github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cce/v3/partitions/requests.go (about) 1 package partitions 2 3 import ( 4 "reflect" 5 6 "github.com/chnsz/golangsdk" 7 ) 8 9 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 10 MoreHeaders: map[string]string{"Content-Type": "application/json"}, 11 } 12 13 // ListOpts allows the filtering of list data using given parameters. 14 type ListOpts struct { 15 Name string `json:"name"` 16 } 17 18 // List returns collection of partitions. 19 func List(client *golangsdk.ServiceClient, clusterID string, opts ListOpts) ([]Partitions, error) { 20 var r ListResult 21 _, r.Err = client.Get(rootURL(client, clusterID), &r.Body, &golangsdk.RequestOpts{ 22 OkCodes: []int{200}, 23 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 24 }) 25 26 allPartitions, err := r.ExtractPartitions() 27 28 if err != nil { 29 return nil, err 30 } 31 32 return FilterPartitions(allPartitions, opts), nil 33 } 34 35 func FilterPartitions(partitions []Partitions, opts ListOpts) []Partitions { 36 var refinedPartitions []Partitions 37 var matched bool 38 39 m := map[string]FilterStruct{} 40 41 if opts.Name != "" { 42 m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}} 43 } 44 45 if len(m) > 0 && len(partitions) > 0 { 46 for _, partition := range partitions { 47 matched = true 48 49 for key, value := range m { 50 if sVal := GetStructNestedField(&partition, key, value.Driller); !(sVal == value.Value) { 51 matched = false 52 } 53 } 54 if matched { 55 refinedPartitions = append(refinedPartitions, partition) 56 } 57 } 58 } else { 59 refinedPartitions = partitions 60 } 61 return refinedPartitions 62 } 63 64 func GetStructNestedField(v *Partitions, field string, structDriller []string) string { 65 r := reflect.ValueOf(v) 66 for _, drillField := range structDriller { 67 f := reflect.Indirect(r).FieldByName(drillField).Interface() 68 r = reflect.ValueOf(f) 69 } 70 f1 := reflect.Indirect(r).FieldByName(field) 71 return string(f1.String()) 72 } 73 74 type FilterStruct struct { 75 Value string 76 Driller []string 77 } 78 79 // CreateOptsBuilder allows extensions to add additional parameters to the 80 // Create request. 81 type CreateOpts struct { 82 // API type, fixed value Partition 83 Kind string `json:"kind" required:"true"` 84 // API version, fixed value v3 85 ApiVersion string `json:"apiversion" required:"true"` 86 // Metadata required to create a Partition 87 Metadata CreateMetaData `json:"metadata"` 88 // specifications to create a Partition 89 Spec Spec `json:"spec" required:"true"` 90 } 91 92 // Metadata required to create a Partition 93 type CreateMetaData struct { 94 // Partition name 95 Name string `json:"name,omitempty"` 96 // Partition tag, key value pair format 97 Labels map[string]string `json:"labels,omitempty"` 98 // Partition annotation, key value pair format 99 Annotations map[string]string `json:"annotations,omitempty"` 100 } 101 102 // Create accepts a CreateOpts struct and uses the values to create a new 103 // logical Partition. When it is created, the Partition does not have an internal 104 // interface 105 type CreateOptsBuilder interface { 106 ToPartitionCreateMap() (map[string]interface{}, error) 107 } 108 109 // ToPartitionCreateMap builds a create request body from CreateOpts. 110 func (opts CreateOpts) ToPartitionCreateMap() (map[string]interface{}, error) { 111 return golangsdk.BuildRequestBody(opts, "") 112 } 113 114 // Create accepts a CreateOpts struct and uses the values to create a new 115 // logical Partition. 116 func Create(c *golangsdk.ServiceClient, clusterid string, opts CreateOptsBuilder) (r CreateResult) { 117 b, err := opts.ToPartitionCreateMap() 118 if err != nil { 119 r.Err = err 120 return 121 } 122 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}} 123 _, r.Err = c.Post(rootURL(c, clusterid), b, &r.Body, reqOpt) 124 return 125 } 126 127 // Get retrieves a particular partitions based on its unique ID and cluster ID. 128 func Get(c *golangsdk.ServiceClient, clusterid, partitionName string) (r GetResult) { 129 _, r.Err = c.Get(resourceURL(c, clusterid, partitionName), &r.Body, &golangsdk.RequestOpts{ 130 OkCodes: []int{200}, 131 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 132 }) 133 return 134 } 135 136 // UpdateOptsBuilder allows extensions to add additional parameters to the 137 // Update request. 138 type UpdateOptsBuilder interface { 139 ToPartitionUpdateMap() (map[string]interface{}, error) 140 } 141 142 // UpdateOpts contains all the values needed to update a new partition 143 type UpdateOpts struct { 144 Metadata UpdateMetadata `json:"metadata,omitempty"` 145 } 146 147 type UpdateMetadata struct { 148 ContainerNetwork []ContainerNetwork `json:"containerNetwork,omitempty"` 149 } 150 151 // ToPartitionUpdateMap builds an update body based on UpdateOpts. 152 func (opts UpdateOpts) ToPartitionUpdateMap() (map[string]interface{}, error) { 153 return golangsdk.BuildRequestBody(opts, "") 154 } 155 156 // Update allows partitions to be updated. 157 func Update(c *golangsdk.ServiceClient, clusterid, partitionName string, opts UpdateOptsBuilder) (r UpdateResult) { 158 b, err := opts.ToPartitionUpdateMap() 159 if err != nil { 160 r.Err = err 161 return 162 } 163 _, r.Err = c.Put(resourceURL(c, clusterid, partitionName), b, &r.Body, &golangsdk.RequestOpts{ 164 OkCodes: []int{200}, 165 }) 166 return 167 } 168 169 // Delete will permanently delete a particular partition based on its unique Name and cluster ID. 170 func Delete(c *golangsdk.ServiceClient, clusterid, partitionName string) (r DeleteResult) { 171 _, r.Err = c.Delete(resourceURL(c, clusterid, partitionName), &golangsdk.RequestOpts{ 172 OkCodes: []int{200}, 173 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 174 }) 175 return 176 }