github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/bms/v1/baremetalservers/requests.go (about) 1 package baremetalservers 2 3 import ( 4 "encoding/base64" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/openstack/common/tags" 8 ) 9 10 type CreateOpts struct { 11 ImageRef string `json:"imageRef" required:"true"` 12 13 FlavorRef string `json:"flavorRef" required:"true"` 14 15 Name string `json:"name" required:"true"` 16 17 MetaData MetaData `json:"metadata" required:"true"` 18 19 UserData []byte `json:"-"` 20 21 AdminPass string `json:"adminPass,omitempty"` 22 23 KeyName string `json:"key_name,omitempty"` 24 25 SecurityGroups []SecurityGroup `json:"security_groups,omitempty"` 26 27 Nics []Nic `json:"nics" required:"true"` 28 29 AvailabilityZone string `json:"availability_zone" required:"true"` 30 31 VpcId string `json:"vpcid" required:"true"` 32 33 PublicIp *PublicIp `json:"publicip,omitempty"` 34 35 Count int `json:"count,omitempty"` 36 37 RootVolume *RootVolume `json:"root_volume,omitempty"` 38 39 DataVolumes []DataVolume `json:"data_volumes,omitempty"` 40 41 ExtendParam ServerExtendParam `json:"extendparam" required:"true"` 42 43 SchedulerHints *SchedulerHints `json:"os:scheduler_hints,omitempty"` 44 45 ServerTags []tags.ResourceTag `json:"server_tags,omitempty"` 46 } 47 48 type MetaData struct { 49 OpSvcUserId string `json:"op_svc_userid" required:"true"` 50 BYOL string `json:"BYOL,omitempty"` 51 AdminPass string `json:"admin_pass,omitempty"` 52 AgencyName string `json:"agency_name,omitempty"` 53 } 54 55 type SecurityGroup struct { 56 ID string `json:"id" required:"true"` 57 } 58 59 type Nic struct { 60 SubnetId string `json:"subnet_id" required:"true"` 61 IpAddress string `json:"ip_address,omitempty"` 62 } 63 64 type PublicIp struct { 65 Id string `json:"id,omitempty"` 66 Eip *Eip `json:"eip,omitempty"` 67 } 68 69 type RootVolume struct { 70 VolumeType string `json:"volumetype,omitempty"` 71 Size int `json:"size,omitempty"` 72 ClusterID string `json:"cluster_id,omitempty"` 73 ClusterType string `json:"cluster_type,omitempty"` 74 } 75 76 type DataVolume struct { 77 VolumeType string `json:"volumetype" required:"true"` 78 Size int `json:"size" required:"true"` 79 Shareable bool `json:"shareable,omitempty"` 80 ClusterID string `json:"cluster_id,omitempty"` 81 ClusterType string `json:"cluster_type,omitempty"` 82 } 83 84 type ServerExtendParam struct { 85 ChargingMode string `json:"chargingMode,omitempty"` 86 87 RegionID string `json:"regionID,omitempty"` 88 89 PeriodType string `json:"periodType,omitempty"` 90 91 PeriodNum int `json:"periodNum,omitempty"` 92 93 IsAutoRenew string `json:"isAutoRenew,omitempty"` 94 95 IsAutoPay string `json:"isAutoPay,omitempty"` 96 97 EnterpriseProjectId string `json:"enterprise_project_id,omitempty"` 98 } 99 100 type SchedulerHints struct { 101 DecBaremetal string `json:"dec_baremetal,omitempty"` 102 } 103 104 type Eip struct { 105 IpType string `json:"iptype" required:"true"` 106 BandWidth BandWidth `json:"bandwidth" required:"true"` 107 ExtendParam EipExtendParam `json:"extendparam" required:"true"` 108 } 109 110 type BandWidth struct { 111 Name string `json:"name,omitempty"` 112 ShareType string `json:"sharetype" required:"true"` 113 Id string `json:"id,omitempty"` 114 Size int `json:"size" required:"true"` 115 ChargeMode string `json:"chargemode,omitempty"` 116 } 117 118 type EipExtendParam struct { 119 ChargingMode string `json:"chargingMode" required:"true"` 120 } 121 122 // CreateOptsBuilder allows extensions to add additional parameters to the 123 // Create request. 124 type CreateOptsBuilder interface { 125 ToServerCreateMap() (map[string]interface{}, error) 126 } 127 128 // ToServerCreateMap assembles a request body based on the contents of a 129 // CreateOpts. 130 func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) { 131 b, err := golangsdk.BuildRequestBody(opts, "") 132 if err != nil { 133 return nil, err 134 } 135 136 if opts.UserData != nil { 137 var userData string 138 if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil { 139 userData = base64.StdEncoding.EncodeToString(opts.UserData) 140 } else { 141 userData = string(opts.UserData) 142 } 143 b["user_data"] = &userData 144 } 145 146 return map[string]interface{}{"server": b}, nil 147 } 148 149 // CreatePrePaid requests a server to be provisioned to the user in the current tenant. 150 func CreatePrePaid(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r OrderResult) { 151 reqBody, err := opts.ToServerCreateMap() 152 if err != nil { 153 r.Err = err 154 return 155 } 156 157 _, r.Err = client.Post(createURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 158 return 159 } 160 161 // Get retrieves a particular Server based on its unique ID. 162 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 163 _, r.Err = c.Get(getURL(c, id), &r.Body, &golangsdk.RequestOpts{ 164 OkCodes: []int{200}, 165 }) 166 return 167 } 168 169 type UpdateOpts struct { 170 Name string `json:"name,omitempty"` 171 } 172 173 type UpdateOptsBuilder interface { 174 ToServerUpdateMap() (map[string]interface{}, error) 175 } 176 177 func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) { 178 b, err := golangsdk.BuildRequestBody(opts, "") 179 if err != nil { 180 return nil, err 181 } 182 183 return map[string]interface{}{"server": b}, nil 184 } 185 186 func Update(client *golangsdk.ServiceClient, id string, ops UpdateOptsBuilder) (r UpdateResult) { 187 b, err := ops.ToServerUpdateMap() 188 if err != nil { 189 r.Err = err 190 return 191 } 192 193 _, r.Err = client.Put(putURL(client, id), b, nil, &golangsdk.RequestOpts{ 194 OkCodes: []int{200}, 195 }) 196 return 197 }