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

Adding Import support to Search Service #172

Merged
merged 2 commits into from
Jul 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions azurerm/import_arm_search_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

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

ri := acctest.RandInt()
config := testAccAzureRMSearchService_basic(ri)

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

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
49 changes: 34 additions & 15 deletions azurerm/resource_arm_search_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,37 @@ func resourceArmSearchService() *schema.Resource {
Read: resourceArmSearchServiceRead,
Update: resourceArmSearchServiceCreate,
Delete: resourceArmSearchServiceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"location": locationSchema(),

"resource_group_name": &schema.Schema{
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"sku": &schema.Schema{
"sku": {
Type: schema.TypeString,
Required: true,
},

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

"partition_count": &schema.Schema{
"partition_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Expand Down Expand Up @@ -86,10 +89,10 @@ func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) er

createResponse, err := createRequest.Execute()
if err != nil {
return fmt.Errorf("Error creating Search Service: %s", err)
return fmt.Errorf("Error creating Search Service: %+v", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating Search Service: %s", createResponse.Error)
return fmt.Errorf("Error creating Search Service: %+v", createResponse.Error)
}

getSearchServiceCommand := &search.GetSearchService{
Expand All @@ -102,10 +105,10 @@ func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) er

readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading Search Service: %s", err)
return fmt.Errorf("Error reading Search Service: %+v", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading Search Service: %s", readResponse.Error)
return fmt.Errorf("Error reading Search Service: %+v", readResponse.Error)
}
resp := readResponse.Parsed.(*search.GetSearchServiceResponse)

Expand All @@ -118,7 +121,7 @@ func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) er
MinTimeout: 15 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Search Service (%s) to become available: %s", d.Get("name"), err)
return fmt.Errorf("Error waiting for Search Service (%s) to become available: %+v", d.Get("name"), err)
}

d.SetId(*resp.ID)
Expand All @@ -130,27 +133,43 @@ func resourceArmSearchServiceRead(d *schema.ResourceData, meta interface{}) erro
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient

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

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

readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading Search Service: %s", err)
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: %s", readResponse.Error)
return fmt.Errorf("Error reading Search Service: %+v", readResponse.Error)
}

resp := readResponse.Parsed.(*search.GetSearchServiceResponse)
d.Set("sku", resp.Sku)

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

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

if resp.ReplicaCount != nil {
d.Set("replica_count", resp.ReplicaCount)
}

flattenAndSetTags(d, &resp.Tags)

return nil
}

Expand All @@ -163,10 +182,10 @@ func resourceArmSearchServiceDelete(d *schema.ResourceData, meta interface{}) er

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

return nil
Expand Down
60 changes: 31 additions & 29 deletions azurerm/resource_arm_search_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,52 @@ import (
)

func TestAccAzureRMSearchService_basic(t *testing.T) {
resourceName := "azurerm_search_service.test"
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMSearchService_basic, ri, ri)
config := testAccAzureRMSearchService_basic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSearchServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSearchServiceExists("azurerm_search_service.test"),
resource.TestCheckResourceAttr(
"azurerm_search_service.test", "tags.%", "2"),
testCheckAzureRMSearchServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
),
},
},
})
}

func TestAccAzureRMSearchService_updateReplicaCountAndTags(t *testing.T) {
resourceName := "azurerm_search_service.test"
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMSearchService_basic, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMSearchService_updated, ri, ri)
preConfig := testAccAzureRMSearchService_basic(ri)
postConfig := testAccAzureRMSearchService_updated(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSearchServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSearchServiceExists("azurerm_search_service.test"),
resource.TestCheckResourceAttr(
"azurerm_search_service.test", "tags.%", "2"),
resource.TestCheckResourceAttr(
"azurerm_search_service.test", "replica_count", "1"),
testCheckAzureRMSearchServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "replica_count", "1"),
),
},

resource.TestStep{
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSearchServiceExists("azurerm_search_service.test"),
resource.TestCheckResourceAttr(
"azurerm_search_service.test", "tags.%", "1"),
resource.TestCheckResourceAttr(
"azurerm_search_service.test", "replica_count", "2"),
testCheckAzureRMSearchServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "replica_count", "2"),
),
},
},
Expand All @@ -81,10 +78,10 @@ func testCheckAzureRMSearchServiceExists(name string) resource.TestCheckFunc {

readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetSearchService: %s", err)
return fmt.Errorf("Bad: GetSearchService: %+v", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Bad: GetSearchService: %s", readResponse.Error)
return fmt.Errorf("Bad: GetSearchService: %+v", readResponse.Error)
}

return nil
Expand All @@ -104,49 +101,54 @@ func testCheckAzureRMSearchServiceDestroy(s *terraform.State) error {

readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetSearchService: %s", err)
return fmt.Errorf("Bad: GetSearchService: %+v", err)
}

if readResponse.IsSuccessful() {
return fmt.Errorf("Bad: Search Service still exists: %s", readResponse.Error)
return fmt.Errorf("Bad: Search Service still exists: %+v", readResponse.Error)
}
}

return nil
}

var testAccAzureRMSearchService_basic = `
func testAccAzureRMSearchService_basic(rInt int) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG_%d"
location = "West US"
}

resource "azurerm_search_service" "test" {
name = "acctestsearchservice%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US"
location = "${azurerm_resource_group.test.location}"
sku = "standard"

tags {
environment = "staging"
database = "test"
}
}
`
`, rInt, rInt)
}

var testAccAzureRMSearchService_updated = `
func testAccAzureRMSearchService_updated(rInt int) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG_%d"
location = "West US"
}
resource "azurerm_search_service" "test" {
name = "acctestsearchservice%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US"
location = "${azurerm_resource_group.test.location}"
sku = "standard"
replica_count = 2

tags {
environment = "production"
}
}
`
`, rInt, rInt)
}
10 changes: 9 additions & 1 deletion website/docs/r/search_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ resource "azurerm_resource_group" "test" {
resource "azurerm_search_service" "test" {
name = "acceptanceTestSearchService1"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US"
location = "${azurerm_resource_group.test.location}"
sku = "standard"

tags {
Expand Down Expand Up @@ -54,3 +54,11 @@ The following arguments are supported:
The following attributes are exported:

* `id` - The Search Service ID.

## Import

Search Services can be imported using the `resource id`, e.g.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the intention here was to put back ticks just around id?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do, but we should update all of them to match this, as this was C&P'd from an existing page for consistency (example)


```
terraform import azurerm_search_service.service1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Search/searchServices/service1
```