github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_compute_servergroup_v2.go (about)

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