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

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"strings"
     9  
    10  	"github.com/Azure/azure-sdk-for-go/arm/cdn"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceArmCdnProfile() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceArmCdnProfileCreate,
    17  		Read:   resourceArmCdnProfileRead,
    18  		Update: resourceArmCdnProfileUpdate,
    19  		Delete: resourceArmCdnProfileDelete,
    20  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"location": locationSchema(),
    32  
    33  			"resource_group_name": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  
    39  			"sku": {
    40  				Type:         schema.TypeString,
    41  				Required:     true,
    42  				ForceNew:     true,
    43  				ValidateFunc: validateCdnProfileSku,
    44  			},
    45  
    46  			"tags": tagsSchema(),
    47  		},
    48  	}
    49  }
    50  
    51  func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error {
    52  	client := meta.(*ArmClient)
    53  	cdnProfilesClient := client.cdnProfilesClient
    54  
    55  	log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")
    56  
    57  	name := d.Get("name").(string)
    58  	location := d.Get("location").(string)
    59  	resGroup := d.Get("resource_group_name").(string)
    60  	sku := d.Get("sku").(string)
    61  	tags := d.Get("tags").(map[string]interface{})
    62  
    63  	cdnProfile := cdn.Profile{
    64  		Location: &location,
    65  		Tags:     expandTags(tags),
    66  		Sku: &cdn.Sku{
    67  			Name: cdn.SkuName(sku),
    68  		},
    69  	}
    70  
    71  	_, err := cdnProfilesClient.Create(resGroup, name, cdnProfile, make(chan struct{}))
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	read, err := cdnProfilesClient.Get(resGroup, name)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	if read.ID == nil {
    81  		return fmt.Errorf("Cannot read CDN Profile %s (resource group %s) ID", name, resGroup)
    82  	}
    83  
    84  	d.SetId(*read.ID)
    85  
    86  	return resourceArmCdnProfileRead(d, meta)
    87  }
    88  
    89  func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
    90  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
    91  
    92  	id, err := parseAzureResourceID(d.Id())
    93  	if err != nil {
    94  		return err
    95  	}
    96  	resGroup := id.ResourceGroup
    97  	name := id.Path["profiles"]
    98  
    99  	resp, err := cdnProfilesClient.Get(resGroup, name)
   100  	if err != nil {
   101  		if resp.StatusCode == http.StatusNotFound {
   102  			d.SetId("")
   103  			return nil
   104  		}
   105  		return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
   106  	}
   107  
   108  	d.Set("name", name)
   109  	d.Set("resource_group_name", resGroup)
   110  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   111  
   112  	if resp.Sku != nil {
   113  		d.Set("sku", string(resp.Sku.Name))
   114  	}
   115  
   116  	flattenAndSetTags(d, resp.Tags)
   117  
   118  	return nil
   119  }
   120  
   121  func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error {
   122  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
   123  
   124  	if !d.HasChange("tags") {
   125  		return nil
   126  	}
   127  
   128  	name := d.Get("name").(string)
   129  	resGroup := d.Get("resource_group_name").(string)
   130  	newTags := d.Get("tags").(map[string]interface{})
   131  
   132  	props := cdn.ProfileUpdateParameters{
   133  		Tags: expandTags(newTags),
   134  	}
   135  
   136  	_, err := cdnProfilesClient.Update(resGroup, name, props, make(chan struct{}))
   137  	if err != nil {
   138  		return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err)
   139  	}
   140  
   141  	return resourceArmCdnProfileRead(d, meta)
   142  }
   143  
   144  func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error {
   145  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
   146  
   147  	id, err := parseAzureResourceID(d.Id())
   148  	if err != nil {
   149  		return err
   150  	}
   151  	resGroup := id.ResourceGroup
   152  	name := id.Path["profiles"]
   153  
   154  	_, err = cdnProfilesClient.Delete(resGroup, name, make(chan struct{}))
   155  	// TODO: check the status code
   156  
   157  	return err
   158  }
   159  
   160  func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) {
   161  	value := strings.ToLower(v.(string))
   162  	skus := map[string]bool{
   163  		"standard_akamai":  true,
   164  		"premium_verizon":  true,
   165  		"standard_verizon": true,
   166  	}
   167  
   168  	if !skus[value] {
   169  		errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Premium_Verizon, Standard_Verizon or Standard_Akamai"))
   170  	}
   171  	return
   172  }