github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 Timeout: 30 * time.Minute, 123 MinTimeout: 15 * time.Second, 124 } 125 if _, err := stateConf.WaitForState(); err != nil { 126 return fmt.Errorf("Error waiting for Search Service (%s) to become available: %s", d.Get("name"), err) 127 } 128 129 d.SetId(*resp.ID) 130 131 return resourceArmSearchServiceRead(d, meta) 132 } 133 134 func resourceArmSearchServiceRead(d *schema.ResourceData, meta interface{}) error { 135 client := meta.(*ArmClient) 136 rivieraClient := client.rivieraClient 137 138 readRequest := rivieraClient.NewRequestForURI(d.Id()) 139 readRequest.Command = &search.GetSearchService{} 140 141 readResponse, err := readRequest.Execute() 142 if err != nil { 143 return fmt.Errorf("Error reading Search Service: %s", err) 144 } 145 if !readResponse.IsSuccessful() { 146 log.Printf("[INFO] Error reading Search Service %q - removing from state", d.Id()) 147 d.SetId("") 148 return fmt.Errorf("Error reading Search Service: %s", readResponse.Error) 149 } 150 151 resp := readResponse.Parsed.(*search.GetSearchServiceResponse) 152 d.Set("sku", resp.Sku) 153 if resp.PartitionCount != nil { 154 d.Set("partition_count", resp.PartitionCount) 155 } 156 if resp.ReplicaCount != nil { 157 d.Set("replica_count", resp.ReplicaCount) 158 } 159 return nil 160 } 161 162 func resourceArmSearchServiceDelete(d *schema.ResourceData, meta interface{}) error { 163 client := meta.(*ArmClient) 164 rivieraClient := client.rivieraClient 165 166 deleteRequest := rivieraClient.NewRequestForURI(d.Id()) 167 deleteRequest.Command = &search.DeleteSearchService{} 168 169 deleteResponse, err := deleteRequest.Execute() 170 if err != nil { 171 return fmt.Errorf("Error deleting Search Service: %s", err) 172 } 173 if !deleteResponse.IsSuccessful() { 174 return fmt.Errorf("Error deleting Search Service: %s", deleteResponse.Error) 175 } 176 177 return nil 178 }