Ildar Svetlov | d213969 | 2020-11-10 15:16:16 +0400 | [diff] [blame^] | 1 | /* |
| 2 | Package stacks provides operation for working with Heat stacks. A stack is a |
| 3 | group of resources (servers, load balancers, databases, and so forth) |
| 4 | combined to fulfill a useful purpose. Based on a template, Heat orchestration |
| 5 | engine creates an instantiated set of resources (a stack) to run the |
| 6 | application framework or component specified (in the template). A stack is a |
| 7 | running instance of a template. The result of creating a stack is a deployment |
| 8 | of the application framework or component. |
| 9 | |
| 10 | Prepare required import packages |
| 11 | |
| 12 | import ( |
| 13 | "fmt" |
| 14 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
| 15 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack" |
| 16 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/orchestration/v1/stacks" |
| 17 | ) |
| 18 | |
| 19 | Example of Preparing Orchestration client: |
| 20 | |
| 21 | client, err := openstack.NewOrchestrationV1(provider, gophercloud.EndpointOpts{Region: "RegionOne"}) |
| 22 | |
| 23 | Example of List Stack: |
| 24 | all_stack_pages, err := stacks.List(client, nil).AllPages() |
| 25 | if err != nil { |
| 26 | panic(err) |
| 27 | } |
| 28 | |
| 29 | all_stacks, err := stacks.ExtractStacks(all_stack_pages) |
| 30 | if err != nil { |
| 31 | panic(err) |
| 32 | } |
| 33 | |
| 34 | for _, stack := range all_stacks { |
| 35 | fmt.Printf("%+v\n", stack) |
| 36 | } |
| 37 | |
| 38 | |
| 39 | Example to Create an Stack |
| 40 | |
| 41 | // Create Template |
| 42 | t := make(map[string]interface{}) |
| 43 | f, err := ioutil.ReadFile("template.yaml") |
| 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | err = yaml.Unmarshal(f, t) |
| 48 | if err != nil { |
| 49 | panic(err) |
| 50 | } |
| 51 | |
| 52 | template := &stacks.Template{} |
| 53 | template.TE = stacks.TE{ |
| 54 | Bin: f, |
| 55 | } |
| 56 | // Create Environment if needed |
| 57 | t_env := make(map[string]interface{}) |
| 58 | f_env, err := ioutil.ReadFile("env.yaml") |
| 59 | if err != nil { |
| 60 | panic(err) |
| 61 | } |
| 62 | err = yaml.Unmarshal(f_env, t_env) |
| 63 | if err != nil { |
| 64 | panic(err) |
| 65 | } |
| 66 | |
| 67 | env := &stacks.Environment{} |
| 68 | env.TE = stacks.TE{ |
| 69 | Bin: f_env, |
| 70 | } |
| 71 | |
| 72 | // Remember, the priority of parameters you given through |
| 73 | // Parameters is higher than the parameters you provided in EnvironmentOpts. |
| 74 | params := make(map[string]string) |
| 75 | params["number_of_nodes"] = 1 |
| 76 | tags := []string{"example-stack"} |
| 77 | createOpts := &stacks.CreateOpts{ |
| 78 | // The name of the stack. It must start with an alphabetic character. |
| 79 | Name: "testing_group", |
| 80 | // A structure that contains either the template file or url. Call the |
| 81 | // associated methods to extract the information relevant to send in a create request. |
| 82 | TemplateOpts: template, |
| 83 | // A structure that contains details for the environment of the stack. |
| 84 | EnvironmentOpts: env, |
| 85 | // User-defined parameters to pass to the template. |
| 86 | Parameters: params, |
| 87 | // A list of tags to assosciate with the Stack |
| 88 | Tags: tags, |
| 89 | } |
| 90 | |
| 91 | r := stacks.Create(client, createOpts) |
| 92 | //dcreated_stack := stacks.CreatedStack() |
| 93 | if r.Err != nil { |
| 94 | panic(r.Err) |
| 95 | } |
| 96 | created_stack, err := r.Extract() |
| 97 | if err != nil { |
| 98 | panic(err) |
| 99 | } |
| 100 | fmt.Printf("Created Stack: %v", created_stack.ID) |
| 101 | |
| 102 | Example for Get Stack |
| 103 | |
| 104 | get_result := stacks.Get(client, stackName, created_stack.ID) |
| 105 | if get_result.Err != nil { |
| 106 | panic(get_result.Err) |
| 107 | } |
| 108 | stack, err := get_result.Extract() |
| 109 | if err != nil { |
| 110 | panic(err) |
| 111 | } |
| 112 | fmt.Println("Get Stack: Name: ", stack.Name, ", ID: ", stack.ID, ", Status: ", stack.Status) |
| 113 | |
| 114 | Example for Delete Stack |
| 115 | |
| 116 | del_r := stacks.Delete(client, stackName, created_stack.ID) |
| 117 | if del_r.Err != nil { |
| 118 | panic(del_r.Err) |
| 119 | } |
| 120 | fmt.Println("Deleted Stack: ", stackName) |
| 121 | |
| 122 | Summary of Behavior Between Stack Update and UpdatePatch Methods : |
| 123 | |
| 124 | Function | Test Case | Result |
| 125 | |
| 126 | Update() | Template AND Parameters WITH Conflict | Parameter takes priority, parameters are set in raw_template.environment overlay |
| 127 | Update() | Template ONLY | Template updates, raw_template.environment overlay is removed |
| 128 | Update() | Parameters ONLY | No update, template is required |
| 129 | |
| 130 | UpdatePatch() | Template AND Parameters WITH Conflict | Parameter takes priority, parameters are set in raw_template.environment overlay |
| 131 | UpdatePatch() | Template ONLY | Template updates, but raw_template.environment overlay is not removed, existing parameter values will remain |
| 132 | UpdatePatch() | Parameters ONLY | Parameters (raw_template.environment) is updated, excluded values are unchanged |
| 133 | |
| 134 | The PUT Update() function will remove parameters from the raw_template.environment overlay |
| 135 | if they are excluded from the operation, whereas PATCH Update() will never be destructive to the |
| 136 | raw_template.environment overlay. It is not possible to expose the raw_template values with a |
| 137 | patch update once they have been added to the environment overlay with the PATCH verb, but |
| 138 | newly added values that do not have a corresponding key in the overlay will display the |
| 139 | raw_template value. |
| 140 | |
| 141 | Example to Update a Stack Using the Update (PUT) Method |
| 142 | |
| 143 | t := make(map[string]interface{}) |
| 144 | f, err := ioutil.ReadFile("template.yaml") |
| 145 | if err != nil { |
| 146 | panic(err) |
| 147 | } |
| 148 | err = yaml.Unmarshal(f, t) |
| 149 | if err != nil { |
| 150 | panic(err) |
| 151 | } |
| 152 | |
| 153 | template := stacks.Template{} |
| 154 | template.TE = stacks.TE{ |
| 155 | Bin: f, |
| 156 | } |
| 157 | |
| 158 | var params = make(map[string]interface{}) |
| 159 | params["number_of_nodes"] = 2 |
| 160 | |
| 161 | stackName := "my_stack" |
| 162 | stackId := "d68cc349-ccc5-4b44-a17d-07f068c01e5a" |
| 163 | |
| 164 | stackOpts := &stacks.UpdateOpts{ |
| 165 | Parameters: params, |
| 166 | TemplateOpts: &template, |
| 167 | } |
| 168 | |
| 169 | res := stacks.Update(orchestrationClient, stackName, stackId, stackOpts) |
| 170 | if res.Err != nil { |
| 171 | panic(res.Err) |
| 172 | } |
| 173 | |
| 174 | Example to Update a Stack Using the UpdatePatch (PATCH) Method |
| 175 | |
| 176 | var params = make(map[string]interface{}) |
| 177 | params["number_of_nodes"] = 2 |
| 178 | |
| 179 | stackName := "my_stack" |
| 180 | stackId := "d68cc349-ccc5-4b44-a17d-07f068c01e5a" |
| 181 | |
| 182 | stackOpts := &stacks.UpdateOpts{ |
| 183 | Parameters: params, |
| 184 | } |
| 185 | |
| 186 | res := stacks.UpdatePatch(orchestrationClient, stackName, stackId, stackOpts) |
| 187 | if res.Err != nil { |
| 188 | panic(res.Err) |
| 189 | } |
| 190 | |
| 191 | Example YAML Template Containing a Heat::ResourceGroup With Three Nodes |
| 192 | |
| 193 | heat_template_version: 2016-04-08 |
| 194 | |
| 195 | parameters: |
| 196 | number_of_nodes: |
| 197 | type: number |
| 198 | default: 3 |
| 199 | description: the number of nodes |
| 200 | node_flavor: |
| 201 | type: string |
| 202 | default: m1.small |
| 203 | description: node flavor |
| 204 | node_image: |
| 205 | type: string |
| 206 | default: centos7.5-latest |
| 207 | description: node os image |
| 208 | node_network: |
| 209 | type: string |
| 210 | default: my-node-network |
| 211 | description: node network name |
| 212 | |
| 213 | resources: |
| 214 | resource_group: |
| 215 | type: OS::Heat::ResourceGroup |
| 216 | properties: |
| 217 | count: { get_param: number_of_nodes } |
| 218 | resource_def: |
| 219 | type: OS::Nova::Server |
| 220 | properties: |
| 221 | name: my_nova_server_%index% |
| 222 | image: { get_param: node_image } |
| 223 | flavor: { get_param: node_flavor } |
| 224 | networks: |
| 225 | - network: {get_param: node_network} |
| 226 | */ |
Jon Perritt | 90944ca | 2015-02-09 11:50:57 -0700 | [diff] [blame] | 227 | package stacks |