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