Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating azurerm_search_service from Riviera to the Azure SDK for Go #283

Merged
merged 4 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/redis"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/Azure/azure-sdk-for-go/arm/scheduler"
"github.com/Azure/azure-sdk-for-go/arm/search"
"github.com/Azure/azure-sdk-for-go/arm/servicebus"
"github.com/Azure/azure-sdk-for-go/arm/sql"
"github.com/Azure/azure-sdk-for-go/arm/storage"
Expand Down Expand Up @@ -109,6 +110,7 @@ type ArmClient struct {
trafficManagerProfilesClient trafficmanager.ProfilesClient
trafficManagerEndpointsClient trafficmanager.EndpointsClient

searchServicesClient search.ServicesClient
serviceBusNamespacesClient servicebus.NamespacesClient
serviceBusQueuesClient servicebus.QueuesClient
serviceBusTopicsClient servicebus.TopicsClient
Expand Down Expand Up @@ -518,6 +520,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
rdc.Sender = autorest.CreateSender(withRequestLogging())
client.redisClient = rdc

sesc := search.NewServicesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&sesc.Client)
sesc.Authorizer = auth
sesc.Sender = autorest.CreateSender(withRequestLogging())
client.searchServicesClient = sesc

sbnc := servicebus.NewNamespacesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&sbnc.Client)
sbnc.Authorizer = auth
Expand Down
24 changes: 24 additions & 0 deletions azurerm/import_arm_search_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,27 @@ func TestAccAzureRMSearchService_importBasic(t *testing.T) {
},
})
}

func TestAccAzureRMSearchService_importComplete(t *testing.T) {
resourceName := "azurerm_search_service.test"

ri := acctest.RandInt()
config := testAccAzureRMSearchService_complete(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSearchServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
158 changes: 72 additions & 86 deletions azurerm/resource_arm_search_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package azurerm
import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/Azure/azure-sdk-for-go/arm/search"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/search"
"github.com/hashicorp/terraform/helper/validation"
"github.com/jen20/riviera/azure"
)

func resourceArmSearchService() *schema.Resource {
return &schema.Resource{
Create: resourceArmSearchServiceCreate,
Create: resourceArmSearchServiceCreateUpdate,
Read: resourceArmSearchServiceRead,
Update: resourceArmSearchServiceCreate,
Delete: resourceArmSearchServiceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -38,90 +37,72 @@ func resourceArmSearchService() *schema.Resource {
"sku": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(search.Free),
string(search.Basic),
string(search.Standard),
string(search.Standard2),
string(search.Standard3),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"replica_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},

"partition_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},

"tags": tagsSchema(),
"tags": tagsForceNewSchema(),
},
}
}

func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
func resourceArmSearchServiceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).searchServicesClient

name := d.Get("name").(string)
location := d.Get("location").(string)
resourceGroupName := d.Get("resource_group_name").(string)
skuName := d.Get("sku").(string)
tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)

command := &search.CreateOrUpdateSearchService{
Name: d.Get("name").(string),
Location: d.Get("location").(string),
ResourceGroupName: d.Get("resource_group_name").(string),
Tags: *expandedTags,
Sku: search.Sku{
Name: d.Get("sku").(string),

properties := search.Service{
Location: azure.String(location),
Sku: &search.Sku{
Name: search.SkuName(skuName),
},
ServiceProperties: &search.ServiceProperties{},
Tags: expandTags(tags),
}

if v, ok := d.GetOk("replica_count"); ok {
replica_count := v.(int)
command.ReplicaCount = &replica_count
replica_count := int32(v.(int))
properties.ServiceProperties.ReplicaCount = azure.Int32(replica_count)
}

if v, ok := d.GetOk("partition_count"); ok {
partition_count := v.(int)
command.PartitionCount = &partition_count
partition_count := int32(v.(int))
properties.ServiceProperties.PartitionCount = azure.Int32(partition_count)
}

createRequest := rivieraClient.NewRequest()
createRequest.Command = command

createResponse, err := createRequest.Execute()
_, err := client.CreateOrUpdate(resourceGroupName, name, properties, nil)
if err != nil {
return fmt.Errorf("Error creating Search Service: %+v", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating Search Service: %+v", createResponse.Error)
}

getSearchServiceCommand := &search.GetSearchService{
Name: d.Get("name").(string),
ResourceGroupName: d.Get("resource_group_name").(string),
return err
}

readRequest := rivieraClient.NewRequest()
readRequest.Command = getSearchServiceCommand

readResponse, err := readRequest.Execute()
resp, err := client.Get(resourceGroupName, name, nil)
if err != nil {
return fmt.Errorf("Error reading Search Service: %+v", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading Search Service: %+v", 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: 60 * time.Minute,
MinTimeout: 15 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Search Service (%s) to become available: %+v", d.Get("name"), err)
return err
}

d.SetId(*resp.ID)
Expand All @@ -130,63 +111,68 @@ func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) er
}

func resourceArmSearchServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
client := meta.(*ArmClient).searchServicesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
resourceGroup := id.ResourceGroup
name := id.Path["searchServices"]

readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &search.GetSearchService{}

readResponse, err := readRequest.Execute()
resp, err := client.Get(resourceGroup, name, nil)
if err != nil {
if responseWasNotFound(resp.Response) {
log.Printf("[INFO] Error reading Search Service %q - removing from state", d.Id())
d.SetId("")
return nil
}

return fmt.Errorf("Error reading Search Service: %+v", err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading Search Service %q - removing from state", d.Id())
d.SetId("")
return fmt.Errorf("Error reading Search Service: %+v", readResponse.Error)
}

resp := readResponse.Parsed.(*search.GetSearchServiceResponse)

d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("location", azureRMNormalizeLocation(resp.Location))
d.Set("sku", string(resp.Sku.Name))
d.Set("resource_group_name", resourceGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))

if resp.PartitionCount != nil {
d.Set("partition_count", resp.PartitionCount)
if resp.Sku != nil {
d.Set("sku", string(resp.Sku.Name))
}

if resp.ReplicaCount != nil {
d.Set("replica_count", resp.ReplicaCount)
if props := resp.ServiceProperties; props != nil {
if props.PartitionCount != nil {
d.Set("partition_count", int(*props.PartitionCount))
}

if props.ReplicaCount != nil {
d.Set("replica_count", int(*props.ReplicaCount))
}
}

flattenAndSetTags(d, &resp.Tags)
flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmSearchServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
client := meta.(*ArmClient).searchServicesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["searchServices"]

deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &search.DeleteSearchService{}
resp, err := client.Delete(resourceGroup, name, nil)

deleteResponse, err := deleteRequest.Execute()
if err != nil {
if responseWasNotFound(resp) {
return nil
}

return fmt.Errorf("Error deleting Search Service: %+v", err)
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting Search Service: %+v", deleteResponse.Error)
}

return nil
}
Loading