2015-04-30 10:58:45 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
2015-04-30 10:58:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAzureSecurityGroupCreate,
|
|
|
|
Read: resourceAzureSecurityGroupRead,
|
|
|
|
Delete: resourceAzureSecurityGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"label": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"location": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
2015-06-16 19:54:52 +02:00
|
|
|
azureClient := meta.(*Client)
|
|
|
|
mc := azureClient.mgmtClient
|
|
|
|
secGroupClient := azureClient.secGroupClient
|
2015-04-30 10:58:45 +02:00
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
2015-05-18 15:40:45 +02:00
|
|
|
// Compute/set the label
|
|
|
|
label := d.Get("label").(string)
|
|
|
|
if label == "" {
|
|
|
|
label = name
|
|
|
|
}
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
req, err := secGroupClient.CreateNetworkSecurityGroup(
|
2015-04-30 10:58:45 +02:00
|
|
|
name,
|
2015-05-18 15:40:45 +02:00
|
|
|
label,
|
2015-04-30 10:58:45 +02:00
|
|
|
d.Get("location").(string),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Network Security Group %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
if err := mc.WaitForOperation(req, nil); err != nil {
|
2015-04-30 10:58:45 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Network Security Group %s to be created: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(name)
|
|
|
|
|
|
|
|
return resourceAzureSecurityGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
|
2015-06-16 19:54:52 +02:00
|
|
|
secGroupClient := meta.(*Client).secGroupClient
|
2015-04-30 10:58:45 +02:00
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
sg, err := secGroupClient.GetNetworkSecurityGroup(d.Id())
|
2015-04-30 10:58:45 +02:00
|
|
|
if err != nil {
|
2015-06-01 11:18:48 +02:00
|
|
|
if management.IsResourceNotFoundError(err) {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-30 10:58:45 +02:00
|
|
|
return fmt.Errorf("Error retrieving Network Security Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("label", sg.Label)
|
|
|
|
d.Set("location", sg.Location)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-06-16 19:54:52 +02:00
|
|
|
azureClient := meta.(*Client)
|
|
|
|
mc := azureClient.mgmtClient
|
|
|
|
secGroupClient := azureClient.secGroupClient
|
2015-04-30 10:58:45 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Deleting Network Security Group: %s", d.Id())
|
2015-06-16 19:54:52 +02:00
|
|
|
req, err := secGroupClient.DeleteNetworkSecurityGroup(d.Id())
|
2015-04-30 10:58:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Network Security Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the network security group is deleted
|
2015-05-22 15:31:14 +02:00
|
|
|
if err := mc.WaitForOperation(req, nil); err != nil {
|
2015-04-30 10:58:45 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Network Security Group %s to be deleted: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|