github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azure/resource_azure_hosted_service.go (about)

     1  package azure
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/management"
     9  	"github.com/Azure/azure-sdk-for-go/management/hostedservice"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  // resourceAzureHostedService returns the schema.Resource associated to an
    14  // Azure hosted service.
    15  func resourceAzureHostedService() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAzureHostedServiceCreate,
    18  		Read:   resourceAzureHostedServiceRead,
    19  		Update: resourceAzureHostedServiceUpdate,
    20  		Delete: resourceAzureHostedServiceDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:        schema.TypeString,
    25  				Required:    true,
    26  				ForceNew:    true,
    27  				Description: parameterDescriptions["name"],
    28  			},
    29  			"location": &schema.Schema{
    30  				Type:        schema.TypeString,
    31  				Required:    true,
    32  				ForceNew:    true,
    33  				Description: parameterDescriptions["location"],
    34  			},
    35  			"ephemeral_contents": &schema.Schema{
    36  				Type:        schema.TypeBool,
    37  				Required:    true,
    38  				Description: parameterDescriptions["ephemeral_contents"],
    39  			},
    40  			"url": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Computed: true,
    43  			},
    44  			"status": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			},
    48  			"reverse_dns_fqdn": &schema.Schema{
    49  				Type:        schema.TypeString,
    50  				Optional:    true,
    51  				Description: parameterDescriptions["reverse_dns_fqdn"],
    52  			},
    53  			"label": &schema.Schema{
    54  				Type:        schema.TypeString,
    55  				ForceNew:    true,
    56  				Optional:    true,
    57  				Default:     "Made by Terraform.",
    58  				Description: parameterDescriptions["label"],
    59  			},
    60  			"description": &schema.Schema{
    61  				Type:        schema.TypeString,
    62  				ForceNew:    true,
    63  				Optional:    true,
    64  				Description: parameterDescriptions["description"],
    65  			},
    66  			"default_certificate_thumbprint": &schema.Schema{
    67  				Type:        schema.TypeString,
    68  				Computed:    true,
    69  				Optional:    true,
    70  				Description: parameterDescriptions["default_certificate_thumbprint"],
    71  			},
    72  		},
    73  	}
    74  }
    75  
    76  // resourceAzureHostedServiceCreate does all the necessary API calls
    77  // to create a hosted service on Azure.
    78  func resourceAzureHostedServiceCreate(d *schema.ResourceData, meta interface{}) error {
    79  	hostedServiceClient := meta.(*Client).hostedServiceClient
    80  
    81  	serviceName := d.Get("name").(string)
    82  	location := d.Get("location").(string)
    83  	reverseDNS := d.Get("reverse_dns_fqdn").(string)
    84  	description := d.Get("description").(string)
    85  	label := base64.StdEncoding.EncodeToString([]byte(d.Get("label").(string)))
    86  
    87  	err := hostedServiceClient.CreateHostedService(
    88  		hostedservice.CreateHostedServiceParameters{
    89  			ServiceName:    serviceName,
    90  			Location:       location,
    91  			Label:          label,
    92  			Description:    description,
    93  			ReverseDNSFqdn: reverseDNS,
    94  		},
    95  	)
    96  	if err != nil {
    97  		return fmt.Errorf("Failed defining new Azure hosted service: %s", err)
    98  	}
    99  
   100  	d.SetId(serviceName)
   101  	return nil
   102  }
   103  
   104  // resourceAzureHostedServiceRead does all the necessary API calls
   105  // to read the state of a hosted service from Azure.
   106  func resourceAzureHostedServiceRead(d *schema.ResourceData, meta interface{}) error {
   107  	hostedServiceClient := meta.(*Client).hostedServiceClient
   108  
   109  	log.Println("[INFO] Querying for hosted service info.")
   110  	serviceName := d.Get("name").(string)
   111  	hostedService, err := hostedServiceClient.GetHostedService(serviceName)
   112  	if err != nil {
   113  		if management.IsResourceNotFoundError(err) {
   114  			// it means the hosted service was deleted in the meantime,
   115  			// so we must remove it here:
   116  			d.SetId("")
   117  			return nil
   118  		} else {
   119  			return fmt.Errorf("Failed to get hosted service: %s", err)
   120  		}
   121  	}
   122  
   123  	log.Println("[DEBUG] Reading hosted service query result data.")
   124  	d.Set("name", hostedService.ServiceName)
   125  	d.Set("url", hostedService.URL)
   126  	d.Set("location", hostedService.Location)
   127  	d.Set("description", hostedService.Description)
   128  	d.Set("label", hostedService.Label)
   129  	d.Set("status", hostedService.Status)
   130  	d.Set("reverse_dns_fqdn", hostedService.ReverseDNSFqdn)
   131  	d.Set("default_certificate_thumbprint", hostedService.DefaultWinRmCertificateThumbprint)
   132  
   133  	return nil
   134  }
   135  
   136  // resourceAzureHostedServiceUpdate does all the necessary API calls to
   137  // update some settings of a hosted service on Azure.
   138  func resourceAzureHostedServiceUpdate(d *schema.ResourceData, meta interface{}) error {
   139  	// NOTE: although no-op; this is still required in order for updates to
   140  	// ephemeral_contents to be possible.
   141  
   142  	// check if the service still exists:
   143  	return resourceAzureHostedServiceRead(d, meta)
   144  }
   145  
   146  // resourceAzureHostedServiceDelete does all the necessary API calls to
   147  // delete a hosted service from Azure.
   148  func resourceAzureHostedServiceDelete(d *schema.ResourceData, meta interface{}) error {
   149  	azureClient := meta.(*Client)
   150  	mgmtClient := azureClient.mgmtClient
   151  	hostedServiceClient := azureClient.hostedServiceClient
   152  
   153  	log.Println("[INFO] Issuing hosted service deletion.")
   154  	serviceName := d.Get("name").(string)
   155  	ephemeral := d.Get("ephemeral_contents").(bool)
   156  	reqID, err := hostedServiceClient.DeleteHostedService(serviceName, ephemeral)
   157  	if err != nil {
   158  		return fmt.Errorf("Failed issuing hosted service deletion request: %s", err)
   159  	}
   160  
   161  	log.Println("[DEBUG] Awaiting confirmation on hosted service deletion.")
   162  	err = mgmtClient.WaitForOperation(reqID, nil)
   163  	if err != nil {
   164  		return fmt.Errorf("Error on hosted service deletion: %s", err)
   165  	}
   166  
   167  	return nil
   168  }