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

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/jen20/riviera/azure"
     9  	"github.com/jen20/riviera/sql"
    10  )
    11  
    12  func resourceArmSqlFirewallRule() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmSqlFirewallRuleCreate,
    15  		Read:   resourceArmSqlFirewallRuleRead,
    16  		Update: resourceArmSqlFirewallRuleCreate,
    17  		Delete: resourceArmSqlFirewallRuleDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"resource_group_name": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"server_name": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"start_ip_address": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  			},
    45  
    46  			"end_ip_address": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Required: true,
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func resourceArmSqlFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
    55  	client := meta.(*ArmClient)
    56  	rivieraClient := client.rivieraClient
    57  
    58  	createRequest := rivieraClient.NewRequest()
    59  	createRequest.Command = &sql.CreateOrUpdateFirewallRule{
    60  		Name:              d.Get("name").(string),
    61  		ResourceGroupName: d.Get("resource_group_name").(string),
    62  		ServerName:        d.Get("server_name").(string),
    63  		StartIPAddress:    azure.String(d.Get("start_ip_address").(string)),
    64  		EndIPAddress:      azure.String(d.Get("end_ip_address").(string)),
    65  	}
    66  
    67  	createResponse, err := createRequest.Execute()
    68  	if err != nil {
    69  		return fmt.Errorf("Error creating SQL Server Firewall Rule: %s", err)
    70  	}
    71  	if !createResponse.IsSuccessful() {
    72  		return fmt.Errorf("Error creating SQL Server Firewall Rule: %s", createResponse.Error)
    73  	}
    74  
    75  	readRequest := rivieraClient.NewRequest()
    76  	readRequest.Command = &sql.GetFirewallRule{
    77  		Name:              d.Get("name").(string),
    78  		ResourceGroupName: d.Get("resource_group_name").(string),
    79  		ServerName:        d.Get("server_name").(string),
    80  	}
    81  
    82  	readResponse, err := readRequest.Execute()
    83  	if err != nil {
    84  		return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", err)
    85  	}
    86  	if !readResponse.IsSuccessful() {
    87  		return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", readResponse.Error)
    88  	}
    89  
    90  	resp := readResponse.Parsed.(*sql.GetFirewallRuleResponse)
    91  	d.SetId(*resp.ID)
    92  
    93  	return resourceArmSqlFirewallRuleRead(d, meta)
    94  }
    95  
    96  func resourceArmSqlFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
    97  	id, err := parseAzureResourceID(d.Id())
    98  	if err != nil {
    99  		return err
   100  	}
   101  	resGroup := id.ResourceGroup
   102  
   103  	client := meta.(*ArmClient)
   104  	rivieraClient := client.rivieraClient
   105  
   106  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   107  	readRequest.Command = &sql.GetFirewallRule{}
   108  
   109  	readResponse, err := readRequest.Execute()
   110  	if err != nil {
   111  		return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", err)
   112  	}
   113  	if !readResponse.IsSuccessful() {
   114  		log.Printf("[INFO] Error reading SQL Server Firewall Rule %q - removing from state", d.Id())
   115  		d.SetId("")
   116  		return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", readResponse.Error)
   117  	}
   118  
   119  	resp := readResponse.Parsed.(*sql.GetFirewallRuleResponse)
   120  
   121  	d.Set("resource_group_name", resGroup)
   122  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   123  	d.Set("name", resp.Name)
   124  	d.Set("server_name", id.Path["servers"])
   125  	d.Set("start_ip_address", resp.StartIPAddress)
   126  	d.Set("end_ip_address", resp.EndIPAddress)
   127  
   128  	return nil
   129  }
   130  
   131  func resourceArmSqlFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
   132  	client := meta.(*ArmClient)
   133  	rivieraClient := client.rivieraClient
   134  
   135  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   136  	deleteRequest.Command = &sql.DeleteFirewallRule{}
   137  
   138  	deleteResponse, err := deleteRequest.Execute()
   139  	if err != nil {
   140  		return fmt.Errorf("Error deleting SQL Server Firewall Rule: %s", err)
   141  	}
   142  	if !deleteResponse.IsSuccessful() {
   143  		return fmt.Errorf("Error deleting SQL Server Firewall Rule: %s", deleteResponse.Error)
   144  	}
   145  
   146  	return nil
   147  }