github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_networking_secgroup_v2.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  
    11  	"github.com/gophercloud/gophercloud"
    12  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
    13  )
    14  
    15  func resourceNetworkingSecGroupV2() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceNetworkingSecGroupV2Create,
    18  		Read:   resourceNetworkingSecGroupV2Read,
    19  		Delete: resourceNetworkingSecGroupV2Delete,
    20  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"region": &schema.Schema{
    26  				Type:        schema.TypeString,
    27  				Required:    true,
    28  				ForceNew:    true,
    29  				DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
    30  			},
    31  			"name": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  			"description": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  				ForceNew: true,
    40  				Computed: true,
    41  			},
    42  			"tenant_id": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Optional: true,
    45  				ForceNew: true,
    46  				Computed: true,
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func resourceNetworkingSecGroupV2Create(d *schema.ResourceData, meta interface{}) error {
    53  
    54  	config := meta.(*Config)
    55  	networkingClient, err := config.networkingV2Client(d.Get("region").(string))
    56  	if err != nil {
    57  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    58  	}
    59  
    60  	opts := groups.CreateOpts{
    61  		Name:        d.Get("name").(string),
    62  		Description: d.Get("description").(string),
    63  		TenantID:    d.Get("tenant_id").(string),
    64  	}
    65  
    66  	log.Printf("[DEBUG] Create OpenStack Neutron Security Group: %#v", opts)
    67  
    68  	security_group, err := groups.Create(networkingClient, opts).Extract()
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	log.Printf("[DEBUG] OpenStack Neutron Security Group created: %#v", security_group)
    74  
    75  	d.SetId(security_group.ID)
    76  
    77  	return resourceNetworkingSecGroupV2Read(d, meta)
    78  }
    79  
    80  func resourceNetworkingSecGroupV2Read(d *schema.ResourceData, meta interface{}) error {
    81  	log.Printf("[DEBUG] Retrieve information about security group: %s", d.Id())
    82  
    83  	config := meta.(*Config)
    84  	networkingClient, err := config.networkingV2Client(d.Get("region").(string))
    85  	if err != nil {
    86  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    87  	}
    88  
    89  	security_group, err := groups.Get(networkingClient, d.Id()).Extract()
    90  
    91  	if err != nil {
    92  		return CheckDeleted(d, err, "OpenStack Neutron Security group")
    93  	}
    94  
    95  	d.Set("description", security_group.Description)
    96  	d.Set("tenant_id", security_group.TenantID)
    97  	d.Set("name", security_group.Name)
    98  	return nil
    99  }
   100  
   101  func resourceNetworkingSecGroupV2Delete(d *schema.ResourceData, meta interface{}) error {
   102  	log.Printf("[DEBUG] Destroy security group: %s", d.Id())
   103  
   104  	config := meta.(*Config)
   105  	networkingClient, err := config.networkingV2Client(d.Get("region").(string))
   106  	if err != nil {
   107  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   108  	}
   109  
   110  	stateConf := &resource.StateChangeConf{
   111  		Pending:    []string{"ACTIVE"},
   112  		Target:     []string{"DELETED"},
   113  		Refresh:    waitForSecGroupDelete(networkingClient, d.Id()),
   114  		Timeout:    2 * time.Minute,
   115  		Delay:      5 * time.Second,
   116  		MinTimeout: 3 * time.Second,
   117  	}
   118  
   119  	_, err = stateConf.WaitForState()
   120  	if err != nil {
   121  		return fmt.Errorf("Error deleting OpenStack Neutron Security Group: %s", err)
   122  	}
   123  
   124  	d.SetId("")
   125  	return err
   126  }
   127  
   128  func waitForSecGroupDelete(networkingClient *gophercloud.ServiceClient, secGroupId string) resource.StateRefreshFunc {
   129  	return func() (interface{}, string, error) {
   130  		log.Printf("[DEBUG] Attempting to delete OpenStack Security Group %s.\n", secGroupId)
   131  
   132  		r, err := groups.Get(networkingClient, secGroupId).Extract()
   133  		if err != nil {
   134  			if _, ok := err.(gophercloud.ErrDefault404); ok {
   135  				log.Printf("[DEBUG] Successfully deleted OpenStack Neutron Security Group %s", secGroupId)
   136  				return r, "DELETED", nil
   137  			}
   138  			return r, "ACTIVE", err
   139  		}
   140  
   141  		err = groups.Delete(networkingClient, secGroupId).ExtractErr()
   142  		if err != nil {
   143  			if _, ok := err.(gophercloud.ErrDefault404); ok {
   144  				log.Printf("[DEBUG] Successfully deleted OpenStack Neutron Security Group %s", secGroupId)
   145  				return r, "DELETED", nil
   146  			}
   147  			return r, "ACTIVE", err
   148  		}
   149  
   150  		log.Printf("[DEBUG] OpenStack Neutron Security Group %s still active.\n", secGroupId)
   151  		return r, "ACTIVE", nil
   152  	}
   153  }