github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_compute_servergroup_v2.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/servergroups"
     9  )
    10  
    11  func resourceComputeServerGroupV2() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceComputeServerGroupV2Create,
    14  		Read:   resourceComputeServerGroupV2Read,
    15  		Update: nil,
    16  		Delete: resourceComputeServerGroupV2Delete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"region": &schema.Schema{
    20  				Type:        schema.TypeString,
    21  				Required:    true,
    22  				ForceNew:    true,
    23  				DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"),
    24  			},
    25  			"name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				ForceNew: true,
    28  				Required: true,
    29  			},
    30  			"policies": &schema.Schema{
    31  				Type:     schema.TypeList,
    32  				Optional: true,
    33  				ForceNew: true,
    34  				Elem:     &schema.Schema{Type: schema.TypeString},
    35  			},
    36  			"members": &schema.Schema{
    37  				Type:     schema.TypeList,
    38  				Computed: true,
    39  				Elem:     &schema.Schema{Type: schema.TypeString},
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceComputeServerGroupV2Create(d *schema.ResourceData, meta interface{}) error {
    46  	config := meta.(*Config)
    47  	computeClient, err := config.computeV2Client(d.Get("region").(string))
    48  	if err != nil {
    49  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    50  	}
    51  
    52  	createOpts := &servergroups.CreateOpts{
    53  		Name:     d.Get("name").(string),
    54  		Policies: resourceServerGroupPoliciesV2(d),
    55  	}
    56  	log.Printf("[DEBUG] Create Options: %#v", createOpts)
    57  	newSG, err := servergroups.Create(computeClient, createOpts).Extract()
    58  	if err != nil {
    59  		return fmt.Errorf("Error creating ServerGroup: %s", err)
    60  	}
    61  
    62  	d.SetId(newSG.ID)
    63  
    64  	return resourceComputeServerGroupV2Read(d, meta)
    65  }
    66  
    67  func resourceComputeServerGroupV2Read(d *schema.ResourceData, meta interface{}) error {
    68  	config := meta.(*Config)
    69  	computeClient, err := config.computeV2Client(d.Get("region").(string))
    70  	if err != nil {
    71  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    72  	}
    73  
    74  	sg, err := servergroups.Get(computeClient, d.Id()).Extract()
    75  	if err != nil {
    76  		return CheckDeleted(d, err, "server group")
    77  	}
    78  
    79  	log.Printf("[DEBUG] Retrieved ServerGroup %s: %+v", d.Id(), sg)
    80  
    81  	// Set the name
    82  	d.Set("name", sg.Name)
    83  
    84  	// Set the policies
    85  	policies := []string{}
    86  	for _, p := range sg.Policies {
    87  		policies = append(policies, p)
    88  	}
    89  	d.Set("policies", policies)
    90  
    91  	// Set the members
    92  	members := []string{}
    93  	for _, m := range sg.Members {
    94  		members = append(members, m)
    95  	}
    96  	d.Set("members", members)
    97  
    98  	return nil
    99  }
   100  
   101  func resourceComputeServerGroupV2Delete(d *schema.ResourceData, meta interface{}) error {
   102  	config := meta.(*Config)
   103  	computeClient, err := config.computeV2Client(d.Get("region").(string))
   104  	if err != nil {
   105  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
   106  	}
   107  
   108  	log.Printf("[DEBUG] Deleting ServerGroup %s", d.Id())
   109  	if err := servergroups.Delete(computeClient, d.Id()).ExtractErr(); err != nil {
   110  		return fmt.Errorf("Error deleting ServerGroup: %s", err)
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  func resourceServerGroupPoliciesV2(d *schema.ResourceData) []string {
   117  	rawPolicies := d.Get("policies").([]interface{})
   118  	policies := make([]string, len(rawPolicies))
   119  	for i, raw := range rawPolicies {
   120  		policies[i] = raw.(string)
   121  	}
   122  	return policies
   123  }