github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/azurerm/resource_arm_search_service.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/jen20/riviera/search"
    11  )
    12  
    13  func resourceArmSearchService() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmSearchServiceCreate,
    16  		Read:   resourceArmSearchServiceRead,
    17  		Update: resourceArmSearchServiceCreate,
    18  		Delete: resourceArmSearchServiceDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"location": &schema.Schema{
    28  				Type:      schema.TypeString,
    29  				Required:  true,
    30  				ForceNew:  true,
    31  				StateFunc: azureRMNormalizeLocation,
    32  			},
    33  
    34  			"resource_group_name": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"sku": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  			},
    44  
    45  			"replica_count": &schema.Schema{
    46  				Type:     schema.TypeInt,
    47  				Optional: true,
    48  				Computed: true,
    49  			},
    50  
    51  			"partition_count": &schema.Schema{
    52  				Type:     schema.TypeInt,
    53  				Optional: true,
    54  				Computed: true,
    55  			},
    56  
    57  			"tags": tagsSchema(),
    58  		},
    59  	}
    60  }
    61  
    62  func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) error {
    63  	client := meta.(*ArmClient)
    64  	rivieraClient := client.rivieraClient
    65  
    66  	tags := d.Get("tags").(map[string]interface{})
    67  	expandedTags := expandTags(tags)
    68  
    69  	command := &search.CreateOrUpdateSearchService{
    70  		Name:              d.Get("name").(string),
    71  		Location:          d.Get("location").(string),
    72  		ResourceGroupName: d.Get("resource_group_name").(string),
    73  		Tags:              *expandedTags,
    74  		Sku: search.Sku{
    75  			Name: d.Get("sku").(string),
    76  		},
    77  	}
    78  
    79  	if v, ok := d.GetOk("replica_count"); ok {
    80  		replica_count := v.(int)
    81  		command.ReplicaCount = &replica_count
    82  	}
    83  
    84  	if v, ok := d.GetOk("partition_count"); ok {
    85  		partition_count := v.(int)
    86  		command.PartitionCount = &partition_count
    87  	}
    88  
    89  	createRequest := rivieraClient.NewRequest()
    90  	createRequest.Command = command
    91  
    92  	createResponse, err := createRequest.Execute()
    93  	if err != nil {
    94  		return fmt.Errorf("Error creating Search Service: %s", err)
    95  	}
    96  	if !createResponse.IsSuccessful() {
    97  		return fmt.Errorf("Error creating Search Service: %s", createResponse.Error)
    98  	}
    99  
   100  	getSearchServiceCommand := &search.GetSearchService{
   101  		Name:              d.Get("name").(string),
   102  		ResourceGroupName: d.Get("resource_group_name").(string),
   103  	}
   104  
   105  	readRequest := rivieraClient.NewRequest()
   106  	readRequest.Command = getSearchServiceCommand
   107  
   108  	readResponse, err := readRequest.Execute()
   109  	if err != nil {
   110  		return fmt.Errorf("Error reading Search Service: %s", err)
   111  	}
   112  	if !readResponse.IsSuccessful() {
   113  		return fmt.Errorf("Error reading Search Service: %s", readResponse.Error)
   114  	}
   115  	resp := readResponse.Parsed.(*search.GetSearchServiceResponse)
   116  
   117  	log.Printf("[DEBUG] Waiting for Search Service (%s) to become available", d.Get("name"))
   118  	stateConf := &resource.StateChangeConf{
   119  		Pending: []string{"provisioning"},
   120  		Target:  []string{"succeeded"},
   121  		Refresh: azureStateRefreshFunc(*resp.ID, client, getSearchServiceCommand),
   122  		// ¯\_(ツ)_/¯
   123  		Timeout:    30 * time.Minute,
   124  		MinTimeout: 15 * time.Second,
   125  	}
   126  	if _, err := stateConf.WaitForState(); err != nil {
   127  		return fmt.Errorf("Error waiting for Search Service (%s) to become available: %s", d.Get("name"), err)
   128  	}
   129  
   130  	d.SetId(*resp.ID)
   131  
   132  	return resourceArmSearchServiceRead(d, meta)
   133  }
   134  
   135  func resourceArmSearchServiceRead(d *schema.ResourceData, meta interface{}) error {
   136  	client := meta.(*ArmClient)
   137  	rivieraClient := client.rivieraClient
   138  
   139  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   140  	readRequest.Command = &search.GetSearchService{}
   141  
   142  	readResponse, err := readRequest.Execute()
   143  	if err != nil {
   144  		return fmt.Errorf("Error reading Search Service: %s", err)
   145  	}
   146  	if !readResponse.IsSuccessful() {
   147  		log.Printf("[INFO] Error reading Search Service %q - removing from state", d.Id())
   148  		d.SetId("")
   149  		return fmt.Errorf("Error reading Search Service: %s", readResponse.Error)
   150  	}
   151  
   152  	resp := readResponse.Parsed.(*search.GetSearchServiceResponse)
   153  	d.Set("sku", resp.Sku)
   154  	if resp.PartitionCount != nil {
   155  		d.Set("partition_count", resp.PartitionCount)
   156  	}
   157  	if resp.ReplicaCount != nil {
   158  		d.Set("replica_count", resp.ReplicaCount)
   159  	}
   160  	return nil
   161  }
   162  
   163  func resourceArmSearchServiceDelete(d *schema.ResourceData, meta interface{}) error {
   164  	client := meta.(*ArmClient)
   165  	rivieraClient := client.rivieraClient
   166  
   167  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   168  	deleteRequest.Command = &search.DeleteSearchService{}
   169  
   170  	deleteResponse, err := deleteRequest.Execute()
   171  	if err != nil {
   172  		return fmt.Errorf("Error deleting Search Service: %s", err)
   173  	}
   174  	if !deleteResponse.IsSuccessful() {
   175  		return fmt.Errorf("Error deleting Search Service: %s", deleteResponse.Error)
   176  	}
   177  
   178  	return nil
   179  }