github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/mls/v1/instances/requests.go (about) 1 package instances 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", "X-Language": "en-us"}, 9 } 10 11 //CreateOptsBuilder is used for creating instance parameters. 12 //any struct providing the parameters should implement this interface 13 type CreateOptsBuilder interface { 14 ToInstanceCreateMap() (map[string]interface{}, error) 15 } 16 17 //CreateOpts is a struct that contains all the parameters. 18 type CreateOpts struct { 19 Name string `json:"name" required:"true"` 20 21 Version string `json:"version" required:"true"` 22 23 Network NetworkOpts `json:"network" required:"true"` 24 25 Agency string `json:"agency,omitempty"` 26 27 FlavorRef string `json:"flavorRef" required:"true"` 28 29 MrsCluster MrsClusterOpts `json:"mrsCluster" required:"true"` 30 } 31 32 type NetworkOpts struct { 33 VpcId string `json:"vpcId" required:"true"` 34 SubnetId string `json:"subnetId" required:"true"` 35 SecurityGroupId string `json:"securityGroupId,omitempty"` 36 AvailableZone string `json:"availableZone" required:"true"` 37 PublicIP PublicIPOpts `json:"publicIP" required:"true"` 38 } 39 40 type PublicIPOpts struct { 41 BindType string `json:"bindType" required:"true"` 42 } 43 44 type MrsClusterOpts struct { 45 Id string `json:"id" required:"true"` 46 UserName string `json:"userName,omitempty"` 47 UserPassword string `json:"userPassword,omitempty"` 48 } 49 50 func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { 51 return golangsdk.BuildRequestBody(opts, "instance") 52 } 53 54 //Create an instance with given parameters. 55 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 56 b, err := opts.ToInstanceCreateMap() 57 if err != nil { 58 r.Err = err 59 return 60 } 61 62 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 63 OkCodes: []int{202}, 64 MoreHeaders: RequestOpts.MoreHeaders, 65 }) 66 return 67 } 68 69 //delete an instance via id 70 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 71 b := make(map[string]interface{}) 72 _, r.Err = client.DeleteWithBody(resourceURL(client, id), b, &golangsdk.RequestOpts{ 73 OkCodes: []int{202}, 74 }) 75 return 76 } 77 78 //get an instance with detailed information by id 79 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 80 _, r.Err = client.Get(resourceURL(client, id), &r.Body, &golangsdk.RequestOpts{ 81 MoreHeaders: RequestOpts.MoreHeaders, 82 }) 83 return 84 }