1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| package main
import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "time" )
var ( //全局默认参数 VolumeType = "sata" AvailableZone = "nova" TemplateVersion = "2018-08-31" ) type PostBody struct { Files interface{} `json:"files"` DisableRollback bool `json:"disable_rollback"` Parameters interface{} `json:"parameters"` StackName string `json:"stack_name"` Template Template `json:"template"` Environment interface{} `json:"environment"` } type Template struct { HeatTemplateVersion string `json:"heat_template_version"` Description string `json:"description"` Parameters interface{} `json:"parameters"` Resources interface{} `json:"resources"` } type VmBody struct { Name string `json:"name"` ImageId string `json:"image_id"` NetworkId string `json:"network_id"` Flavor string `json:"flavor"` DiskSize string `json:"disk_size"` SecurityGroupId string `json:"security_group_id"` } //Resource相关 type ResourceServer struct { Type string `json:"type"` Properties ServerProperty `json:"properties"` } type ServerProperty struct { Name string `json:"name"` Image string `json:"image"` Flavor string `json:"flavor"` Networks []ServerNetworkProperty `json:"networks"` SecurityGroups []string `json:"security_groups"` } type ServerNetworkProperty struct { Network string `json:"network"` } type ResourceVolume struct { Type string `json:"type"` Properties VolumeProperty `json:"properties"` } type VolumeProperty struct { Size string `json:"size"` VolumeType string `json:"volume_type"` AvailabilityZone string `json:"availability_zone"` Description string `json:"description"` } type ResourceVolumeAttach struct { Type string `json:"type"` Properties VolumeAttachProperties `json:"properties"` } type VolumeAttachProperties struct { VolumeId PropertyInfo `json:"volume_id"` InstanceUuid PropertyInfo `json:"instance_uuid"` } type PropertyInfo struct{ GetResource string `json:"get_resource"` } func CreateStack(stackName string,){
} //测试 heat 创建 func main() { //前端传来 [{"name":"server1","flavor":"ecs.small","image_id":"xxxxxxx","network_id":"xxxxx"}, //{"name":"server2","flavor":"ecs.large","image_id":"xxxx","network_id":"xxxxx"}]
var ServerList []VmBody ServerList = append(ServerList, VmBody{ Name: "server1", Flavor: "ecs.small", ImageId: "67e7da07-e9d6-4f24-840e-205259d27913", NetworkId: "6582c415-3578-4dd4-ac45-8920a4194462", DiskSize: "50", SecurityGroupId: "c7ff8d7e-de91-41cc-90e6-76d8d51d2688",
}) ServerList = append(ServerList, VmBody{ Name: "server2", Flavor: "ecs.large", ImageId: "84ae6cba-7e00-4f6c-907f-3e6832e6d825", NetworkId: "6582c415-3578-4dd4-ac45-8920a4194462", DiskSize: "100", SecurityGroupId: "c7ff8d7e-de91-41cc-90e6-76d8d51d2688", }) stackName := "zetao-test2" regionName := "RegionOne" //这里是以cinder云盘为例,本地盘请不要传入volume相关即可~ var resourcesServerMap map[string]interface{} var heatTemplate Template var postBody PostBody resourcesServerMap = make(map[string]interface{})
for _, s := range ServerList { var serverNetworks []ServerNetworkProperty var tmpServerNetwork ServerNetworkProperty var securityGroups []string tmpServerNetwork=ServerNetworkProperty{ Network: s.NetworkId, } serverNetworks=append(serverNetworks,tmpServerNetwork) securityGroups=append(securityGroups,s.SecurityGroupId) resourcesServerMap[s.Name] = ResourceServer{ Type: "OS::Nova::Server", Properties: ServerProperty{ Name: s.Name, Flavor: s.Flavor, Image: s.ImageId, Networks: serverNetworks, SecurityGroups: securityGroups, }, } serverVolumeName := fmt.Sprintf("%sVolume", s.Name) resourcesServerMap[serverVolumeName] = ResourceVolume{ Type: "OS::Cinder::Volume", Properties: VolumeProperty{ Size: s.DiskSize, VolumeType: VolumeType, AvailabilityZone: AvailableZone, Description: serverVolumeName, }, } serverVolumeAttachName := fmt.Sprintf("%sVolume_attachment", s.Name) //主机volume attach info resourcesServerMap[serverVolumeAttachName] = ResourceVolumeAttach{ Type: "OS::Cinder::VolumeAttachment", Properties: VolumeAttachProperties{ VolumeId: PropertyInfo{ GetResource: serverVolumeName, }, InstanceUuid: PropertyInfo{ GetResource: s.Name, }, }, } }
heatTemplate = Template{ HeatTemplateVersion: TemplateVersion, Description: "Heat Template", Parameters: struct { }{}, Resources: resourcesServerMap, } postBody = PostBody{ Files: struct {
}{}, DisableRollback: true, Parameters: struct {
}{}, StackName: stackName, Template: heatTemplate, Environment: struct { }{}, }
b, _ := json.Marshal(postBody) fmt.Println(string(b)) //发起post请求 OpenstackToken := "gAAAAABgN1v2CCL09LAS3aokDkccXuLAn4RZx60z-vLdzDkXyw5oX6HKsXmoKWBaKOY27RcRr4SG1igRC68nHJWtwfGUbs4kMH2ultNCxAKgQz3KZEIXJ6L2gc832L0g_ngyzm93bovWwl8iMqWV7oZzBtGqsB8nIhVvSZqq2M6BzfWBc1cV980" postUrl := "http://10.19.114.193:8004/v1/319bb16c497f422591f0688b2aec3f76/stacks" req, err := http.NewRequest("POST",postUrl, bytes.NewBuffer(b)) //req.Header.Set("User-Agent","python-heatclient") req.Header.Set("Accept","application/json") req.Header.Set("Content-Type","application/json") req.Header.Set("X-Auth-Token",OpenstackToken) req.Header.Set("X-Region-Name",regionName)
if err != nil { fmt.Println(err) return } // Set client timeout client := &http.Client{Timeout: time.Second * time.Duration(5)} // Send request resp, err := client.Do(req) if err != nil { fmt.Println("Error reading response.", err) return } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) //对body序列化 fmt.Println(string(body))
}
|