diff --git a/builtin/providers/azurerm/provider.go b/builtin/providers/azurerm/provider.go index 115699b32..744e4128a 100644 --- a/builtin/providers/azurerm/provider.go +++ b/builtin/providers/azurerm/provider.go @@ -4,13 +4,16 @@ import ( "fmt" "log" "net/http" + "reflect" "strings" "github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/helper/mutexkv" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" + riviera "github.com/jen20/riviera/azure" ) // Provider returns a terraform.ResourceProvider. @@ -71,7 +74,6 @@ func Provider() terraform.ResourceProvider { "azurerm_sql_server": resourceArmSqlServer(), "azurerm_sql_database": resourceArmSqlDatabase(), "azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(), - "azurerm_search_service": resourceArmSearchService(), }, ConfigureFunc: providerConfigure, } @@ -139,7 +141,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { func registerAzureResourceProvidersWithSubscription(config *Config, client *ArmClient) error { providerClient := client.providers - providers := []string{"Microsoft.Network", "Microsoft.Compute", "Microsoft.Cdn", "Microsoft.Storage", "Microsoft.Sql", "Microsoft.Search"} + providers := []string{"Microsoft.Network", "Microsoft.Compute", "Microsoft.Cdn", "Microsoft.Storage", "Microsoft.Sql"} for _, v := range providers { res, err := providerClient.Register(v) @@ -199,3 +201,32 @@ func pollIndefinitelyAsNeeded(client autorest.Client, response *http.Response, a // armMutexKV is the instance of MutexKV for ARM resources var armMutexKV = mutexkv.NewMutexKV() + +func azureStateRefreshFunc(resourceURI string, client *ArmClient, command riviera.APICall) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + req := client.rivieraClient.NewRequestForURI(resourceURI) + req.Command = command + + res, err := req.Execute() + if err != nil { + return nil, "", fmt.Errorf("Error executing %T command in azureStateRefreshFunc", req.Command) + } + + var value reflect.Value + if reflect.ValueOf(res.Parsed).Kind() == reflect.Ptr { + value = reflect.ValueOf(res.Parsed).Elem() + } else { + value = reflect.ValueOf(res.Parsed) + } + + for i := 0; i < value.NumField(); i++ { // iterates through every struct type field + tag := value.Type().Field(i).Tag // returns the tag string + tagValue := tag.Get("mapstructure") + if tagValue == "provisioningState" { + return res.Parsed, value.Field(i).Elem().String(), nil + } + } + + panic(fmt.Errorf("azureStateRefreshFunc called on structure %T with no mapstructure:provisioningState tag. This is a bug", res.Parsed)) + } +} diff --git a/builtin/providers/azurerm/resource_arm_search_service.go b/builtin/providers/azurerm/resource_arm_search_service.go index d77499487..4483044af 100644 --- a/builtin/providers/azurerm/resource_arm_search_service.go +++ b/builtin/providers/azurerm/resource_arm_search_service.go @@ -3,7 +3,9 @@ package azurerm import ( "fmt" "log" + "time" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/jen20/riviera/azure" "github.com/jen20/riviera/search" @@ -94,12 +96,14 @@ func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) er return fmt.Errorf("Error creating Search Service: %s", createResponse.Error) } - readRequest := rivieraClient.NewRequest() - readRequest.Command = &search.GetSearchService{ + getSearchServiceCommand := &search.GetSearchService{ Name: d.Get("name").(string), ResourceGroupName: d.Get("resource_group_name").(string), } + readRequest := rivieraClient.NewRequest() + readRequest.Command = getSearchServiceCommand + readResponse, err := readRequest.Execute() if err != nil { return fmt.Errorf("Error reading Search Service: %s", err) @@ -107,8 +111,20 @@ func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) er if !readResponse.IsSuccessful() { return fmt.Errorf("Error reading Search Service: %s", readResponse.Error) } - resp := readResponse.Parsed.(*search.GetSearchServiceResponse) + + log.Printf("[DEBUG] Waiting for Search Service (%s) to become available", d.Get("name")) + stateConf := &resource.StateChangeConf{ + Pending: []string{"provisioning"}, + Target: []string{"succeeded"}, + Refresh: azureStateRefreshFunc(*resp.ID, client, getSearchServiceCommand), + // ¯\_(ツ)_/¯ + Timeout: 30 * time.Minute, + } + if _, err := stateConf.WaitForState(); err != nil { + return fmt.Errorf("Error waiting for Search Service (%s) to become available: %s", d.Get("name"), err) + } + d.SetId(*resp.ID) return resourceArmSearchServiceRead(d, meta)