github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/iec/v1/vpcs/requests.go (about) 1 package vpcs 2 3 import ( 4 "net/http" 5 6 "github.com/huaweicloud/golangsdk" 7 ) 8 9 // CreateOptsBuilder allows extensions to add additional parameters to the 10 // create request. 11 type CreateOptsBuilder interface { 12 ToVPCCreateMap() (map[string]interface{}, error) 13 } 14 type CreateOpts struct { 15 //vpc name 16 Name string `json:"name" required:"true"` 17 //cidr,172.16.0.0/12~172.248.255.0/24 18 Cidr string `json:"cidr" required:"true"` 19 20 //mode,SYSTEM or CUSTOMER,SYSTEM: system will design and create subnet when you need; 21 // CUSTOMER: you should design and create by yourself 22 Mode string `json:"mode" required:"true"` 23 } 24 25 // ToSecurityGroupsCreateMap converts CreateOpts structures to map[string]interface{} 26 func (opts CreateOpts) ToVPCCreateMap() (map[string]interface{}, error) { 27 b, err := golangsdk.BuildRequestBody(&opts, "vpc") 28 if err != nil { 29 return nil, err 30 } 31 return b, nil 32 } 33 34 type UpdateOpts struct { 35 // Specifies the name of the VPC. The name must be unique for a 36 // tenant. The value is a string of no more than 64 characters and can contain digits, 37 // letters, underscores (_), and hyphens (-). 38 Name string `json:"name,omitempty"` 39 40 // Specifies the range of available subnets in the VPC. The value 41 // must be in CIDR format, for example, 192.168.0.0/16. The value ranges from 10.0.0.0/8 42 // to 10.255.255.0/24, 172.16.0.0/12 to 172.31.255.0/24, or 192.168.0.0/16 to 43 // 192.168.255.0/24. 44 Cidr string `json:"cidr,omitempty"` 45 } 46 47 type UpdateOptsBuilder interface { 48 ToVPCUpdateMap() (map[string]interface{}, error) 49 } 50 51 func (opts UpdateOpts) ToVPCUpdateMap() (map[string]interface{}, error) { 52 b, err := golangsdk.BuildRequestBody(opts, "vpc") 53 if err != nil { 54 return nil, err 55 } 56 return b, nil 57 } 58 59 // Get get vpc detail 60 func Get(client *golangsdk.ServiceClient, vpcID string) (r GetResult) { 61 getURL := GetURL(client, vpcID) 62 63 var resp *http.Response 64 resp, r.Err = client.Get(getURL, &r.Body, &golangsdk.RequestOpts{ 65 OkCodes: []int{http.StatusOK}, 66 }) 67 if r.Err != nil { 68 return 69 } 70 defer resp.Body.Close() 71 72 return 73 } 74 75 // Create create vpc 76 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 77 b, err := opts.ToVPCCreateMap() 78 if err != nil { 79 r.Err = err 80 return 81 } 82 createURL := rootURL(client) 83 84 var resp *http.Response 85 resp, r.Err = client.Post(createURL, b, &r.Body, &golangsdk.RequestOpts{ 86 OkCodes: []int{http.StatusOK}, 87 }) 88 if r.Err != nil { 89 return 90 } 91 defer resp.Body.Close() 92 93 return 94 } 95 96 //Update update vpc info,name especially 97 func Update(client *golangsdk.ServiceClient, vpcID string, opts UpdateOptsBuilder) (r UpdateResult) { 98 b, err := opts.ToVPCUpdateMap() 99 if err != nil { 100 r.Err = err 101 return 102 } 103 updateURL := UpdateURL(client, vpcID) 104 105 var resp *http.Response 106 resp, r.Err = client.Put(updateURL, b, &r.Body, &golangsdk.RequestOpts{ 107 OkCodes: []int{http.StatusOK}, 108 }) 109 if r.Err != nil { 110 return 111 } 112 defer resp.Body.Close() 113 114 return 115 } 116 117 //Delete delete the vpc 118 func Delete(client *golangsdk.ServiceClient, vpcID string) (r DeleteResult) { 119 deleteURL := DeleteURL(client, vpcID) 120 121 var resp *http.Response 122 resp, r.Err = client.Delete(deleteURL, &golangsdk.RequestOpts{ 123 OkCodes: []int{http.StatusNoContent}, 124 }) 125 if r.Err != nil { 126 return 127 } 128 defer resp.Body.Close() 129 130 return 131 } 132 133 // ListOpts allows the filtering collections through the API. 134 type ListOpts struct { 135 Limit int `q:"limit"` 136 Offset int `q:"offset"` 137 138 // ID is the unique identifier for the vpc. 139 ID string `q:"id"` 140 141 // Name is the human readable name for the vpc. It does not have to be unique. 142 Name string `q:"name"` 143 } 144 145 type ListVPCOptsBuilder interface { 146 ToListVPCQuery() (string, error) 147 } 148 149 // ToListVPCQuery converts ListVPCOpts structures to query string 150 func (opts ListOpts) ToListVPCQuery() (string, error) { 151 q, err := golangsdk.BuildQueryString(opts) 152 if err != nil { 153 return "", err 154 } 155 156 return q.String(), err 157 } 158 159 // List returns collection of vpcs. 160 // It accepts a ListOpts struct, which allows you to filter 161 // the returned collection for greater efficiency. 162 func List(client *golangsdk.ServiceClient, opts ListVPCOptsBuilder) (r ListResult) { 163 listURL := rootURL(client) 164 if opts != nil { 165 query, err := opts.ToListVPCQuery() 166 if err != nil { 167 r.Err = err 168 return 169 } 170 listURL += query 171 } 172 173 _, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{ 174 OkCodes: []int{http.StatusOK}, 175 }) 176 return 177 }