github.com/gophercloud/gophercloud@v1.11.0/openstack/orchestration/v1/stacks/doc.go (about)

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