github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cci/v1/networks/requests.go (about) 1 package networks 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 ) 6 7 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 8 MoreHeaders: map[string]string{"Content-Type": "application/json"}, 9 } 10 11 // CreateOptsBuilder allows extensions to add additional parameters to the 12 // Create request. 13 type CreateOptsBuilder interface { 14 ToNetworkCreateMap() (map[string]interface{}, error) 15 } 16 17 // CreateOpts contains all the values needed to create a new network 18 type CreateOpts struct { 19 // API type, fixed value Network 20 Kind string `json:"kind" required:"true"` 21 // API version, fixed value networking.cci.io 22 ApiVersion string `json:"apiVersion" required:"true"` 23 // Metadata required to create a network 24 Metadata CreateMetaData `json:"metadata" required:"true"` 25 // Specifications to create a network 26 Spec Spec `json:"spec" required:"true"` 27 } 28 29 // Metadata required to create a network 30 type CreateMetaData struct { 31 //Network unique name 32 Name string `json:"name" required:"true"` 33 //Network annotation, key/value pair format 34 Annotations map[string]string `json:"annotations" required:"true"` 35 } 36 37 // Specifications to create a network 38 type Spec struct { 39 // Network CIDR 40 Cidr string `json:"type,omitempty"` 41 // Network VPC ID 42 AttachedVPC string `json:"attachedVPC" required:"true"` 43 // Network Type 44 NetworkType string `json:"networkType" required:"true"` 45 // Network ID 46 NetworkID string `json:"networkID" required:"true"` 47 // Subnet ID 48 SubnetID string `json:"subnetID" required:"true"` 49 // Network AZ 50 AvailableZone string `json:"availableZone" required:"true"` 51 } 52 53 // ToNetworkCreateMap builds a create request body from CreateOpts. 54 func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) { 55 return golangsdk.BuildRequestBody(opts, "") 56 } 57 58 // Create accepts a CreateOpts struct and uses the values to create a new network. 59 func Create(c *golangsdk.ServiceClient, ns string, opts CreateOptsBuilder) (r CreateResult) { 60 b, err := opts.ToNetworkCreateMap() 61 if err != nil { 62 r.Err = err 63 return 64 } 65 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}} 66 _, r.Err = c.Post(rootURL(c, ns), b, &r.Body, reqOpt) 67 return 68 } 69 70 // Get retrieves a particular network based on its unique ID. 71 func Get(c *golangsdk.ServiceClient, ns, id string) (r GetResult) { 72 _, r.Err = c.Get(resourceURL(c, ns, id), &r.Body, &golangsdk.RequestOpts{ 73 OkCodes: []int{200}, 74 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 75 }) 76 return 77 } 78 79 // Delete will permanently delete a particular network based on its unique ID. 80 func Delete(c *golangsdk.ServiceClient, ns, id string) (r DeleteResult) { 81 _, r.Err = c.Delete(resourceURL(c, ns, id), &golangsdk.RequestOpts{ 82 OkCodes: []int{200}, 83 MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, 84 }) 85 return 86 }