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

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/management"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceAzureSecurityGroup() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceAzureSecurityGroupCreate,
    14  		Read:   resourceAzureSecurityGroupRead,
    15  		Delete: resourceAzureSecurityGroupDelete,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"name": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  
    24  			"label": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Optional: true,
    27  				Computed: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"location": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{}) (err error) {
    41  	azureClient := meta.(*Client)
    42  	mc := azureClient.mgmtClient
    43  	secGroupClient := azureClient.secGroupClient
    44  
    45  	name := d.Get("name").(string)
    46  
    47  	// Compute/set the label
    48  	label := d.Get("label").(string)
    49  	if label == "" {
    50  		label = name
    51  	}
    52  
    53  	req, err := secGroupClient.CreateNetworkSecurityGroup(
    54  		name,
    55  		label,
    56  		d.Get("location").(string),
    57  	)
    58  	if err != nil {
    59  		return fmt.Errorf("Error creating Network Security Group %s: %s", name, err)
    60  	}
    61  
    62  	if err := mc.WaitForOperation(req, nil); err != nil {
    63  		return fmt.Errorf(
    64  			"Error waiting for Network Security Group %s to be created: %s", name, err)
    65  	}
    66  
    67  	d.SetId(name)
    68  
    69  	return resourceAzureSecurityGroupRead(d, meta)
    70  }
    71  
    72  func resourceAzureSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
    73  	secGroupClient := meta.(*Client).secGroupClient
    74  
    75  	sg, err := secGroupClient.GetNetworkSecurityGroup(d.Id())
    76  	if err != nil {
    77  		if management.IsResourceNotFoundError(err) {
    78  			d.SetId("")
    79  			return nil
    80  		}
    81  		return fmt.Errorf("Error retrieving Network Security Group %s: %s", d.Id(), err)
    82  	}
    83  
    84  	d.Set("label", sg.Label)
    85  	d.Set("location", sg.Location)
    86  
    87  	return nil
    88  }
    89  
    90  func resourceAzureSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
    91  	azureClient := meta.(*Client)
    92  	mc := azureClient.mgmtClient
    93  	secGroupClient := azureClient.secGroupClient
    94  
    95  	log.Printf("[DEBUG] Deleting Network Security Group: %s", d.Id())
    96  	req, err := secGroupClient.DeleteNetworkSecurityGroup(d.Id())
    97  	if err != nil {
    98  		return fmt.Errorf("Error deleting Network Security Group %s: %s", d.Id(), err)
    99  	}
   100  
   101  	// Wait until the network security group is deleted
   102  	if err := mc.WaitForOperation(req, nil); err != nil {
   103  		return fmt.Errorf(
   104  			"Error waiting for Network Security Group %s to be deleted: %s", d.Id(), err)
   105  	}
   106  
   107  	d.SetId("")
   108  
   109  	return nil
   110  }