Merge pull request #5203 from hashicorp/f-azurerm-search-service

provider/azurerm: Add Search Service resource
This commit is contained in:
James Nugent 2016-02-22 13:53:02 -05:00
commit 8e4691d895
10 changed files with 542 additions and 5 deletions

12
Godeps/Godeps.json generated
View File

@ -536,16 +536,20 @@
},
{
"ImportPath": "github.com/jen20/riviera/azure",
"Rev": "31f4644de1f4931e43271240069bf2b896f47005"
"Rev": "64de55fa8cdd0c52f7d59494c1b03c1b583c52b4"
},
{
"ImportPath": "github.com/jen20/riviera/dns",
"Rev": "31f4644de1f4931e43271240069bf2b896f47005"
"Rev": "64de55fa8cdd0c52f7d59494c1b03c1b583c52b4"
},
{
"ImportPath": "github.com/jen20/riviera/search",
"Rev": "64de55fa8cdd0c52f7d59494c1b03c1b583c52b4"
},
{
"ImportPath": "github.com/jen20/riviera/sql",
"Rev": "31f4644de1f4931e43271240069bf2b896f47005"
},
"Rev": "64de55fa8cdd0c52f7d59494c1b03c1b583c52b4"
},
{
"ImportPath": "github.com/jmespath/go-jmespath",
"Comment": "0.2.2-2-gc01cf91",

View File

@ -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,6 +74,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_sql_server": resourceArmSqlServer(),
"azurerm_sql_database": resourceArmSqlDatabase(),
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
"azurerm_search_service": resourceArmSearchService(),
},
ConfigureFunc: providerConfigure,
}
@ -138,7 +142,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"}
providers := []string{"Microsoft.Network", "Microsoft.Compute", "Microsoft.Cdn", "Microsoft.Storage", "Microsoft.Sql", "Microsoft.Search"}
for _, v := range providers {
res, err := providerClient.Register(v)
@ -198,3 +202,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))
}
}

View File

@ -0,0 +1,179 @@
package azurerm
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/search"
)
func resourceArmSearchService() *schema.Resource {
return &schema.Resource{
Create: resourceArmSearchServiceCreate,
Read: resourceArmSearchServiceRead,
Update: resourceArmSearchServiceCreate,
Delete: resourceArmSearchServiceDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: azureRMNormalizeLocation,
},
"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sku": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"replica_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"partition_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmSearchServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
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),
},
}
if v, ok := d.GetOk("replica_count"); ok {
replica_count := v.(int)
command.ReplicaCount = &replica_count
}
if v, ok := d.GetOk("partition_count"); ok {
partition_count := v.(int)
command.PartitionCount = &partition_count
}
createRequest := rivieraClient.NewRequest()
createRequest.Command = command
createResponse, err := createRequest.Execute()
if err != nil {
return fmt.Errorf("Error creating Search Service: %s", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating Search Service: %s", createResponse.Error)
}
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)
}
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,
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)
}
d.SetId(*resp.ID)
return resourceArmSearchServiceRead(d, meta)
}
func resourceArmSearchServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
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)
}
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)
}
resp := readResponse.Parsed.(*search.GetSearchServiceResponse)
d.Set("sku", resp.Sku)
if resp.PartitionCount != nil {
d.Set("partition_count", resp.PartitionCount)
}
if resp.ReplicaCount != nil {
d.Set("replica_count", resp.ReplicaCount)
}
return nil
}
func resourceArmSearchServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &search.DeleteSearchService{}
deleteResponse, err := deleteRequest.Execute()
if err != nil {
return fmt.Errorf("Error deleting Search Service: %s", err)
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting Search Service: %s", deleteResponse.Error)
}
return nil
}

View File

@ -0,0 +1,152 @@
package azurerm
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/jen20/riviera/search"
)
func TestAccAzureRMSearchService_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMSearchService_basic, ri, 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"),
),
},
},
})
}
func TestAccAzureRMSearchService_updateReplicaCountAndTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMSearchService_basic, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMSearchService_updated, ri, 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"),
),
},
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"),
),
},
},
})
}
func testCheckAzureRMSearchServiceExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &search.GetSearchService{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetSearchService: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Bad: GetSearchService: %s", readResponse.Error)
}
return nil
}
}
func testCheckAzureRMSearchServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_search_service" {
continue
}
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &search.GetSearchService{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetSearchService: %s", err)
}
if readResponse.IsSuccessful() {
return fmt.Errorf("Bad: Search Service still exists: %s", readResponse.Error)
}
}
return nil
}
var testAccAzureRMSearchService_basic = `
resource "azurerm_resource_group" "test" {
name = "acctest_rg_%d"
location = "West US"
}
resource "azurerm_search_service" "test" {
name = "acctestsearchservice%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US"
sku = "standard"
tags {
environment = "staging"
database = "test"
}
}
`
var testAccAzureRMSearchService_updated = `
resource "azurerm_resource_group" "test" {
name = "acctest_rg_%d"
location = "West US"
}
resource "azurerm_search_service" "test" {
name = "acctestsearchservice%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US"
sku = "standard"
replica_count = 2
tags {
environment = "production"
}
}
`

12
vendor/github.com/jen20/riviera/search/api.go generated vendored Normal file
View File

@ -0,0 +1,12 @@
package search
import "fmt"
const apiVersion = "2015-08-19"
const apiProvider = "Microsoft.Search"
func searchServiceDefaultURLPath(resourceGroupName, serviceName string) func() string {
return func() string {
return fmt.Sprintf("resourceGroups/%s/providers/%s/searchServices/%s", resourceGroupName, apiProvider, serviceName)
}
}

View File

@ -0,0 +1,41 @@
package search
import "github.com/jen20/riviera/azure"
type Sku struct {
Name string `json:"name" mapstructure:"name"`
}
type CreateOrUpdateSearchServiceResponse struct {
ID *string `mapstructure:"id"`
Name *string `mapstructure:"name"`
Location *string `mapstructure:"location"`
Tags *map[string]*string `mapstructure:"tags"`
Sku *Sku `mapstructure:"sku"`
ReplicaCount *int `mapstructure:"replicaCount"`
PartitionCount *int `mapstructure:"partitionCount"`
Status *string `mapstructure:"status"`
StatusDetails *string `mapstructure:"statusDetails"`
ProvisioningStatus *string `mapstructure:"provisioningStatus"`
}
type CreateOrUpdateSearchService struct {
Name string `json:"-"`
ResourceGroupName string `json:"-"`
Location string `json:"-" riviera:"location"`
Tags map[string]*string `json:"-" riviera:"tags"`
Sku Sku `json:"-" riviera:"sku"`
ReplicaCount *int `json:"replicaCount,omitempty"`
PartitionCount *int `json:"partitionCount,omitempty"`
}
func (s CreateOrUpdateSearchService) APIInfo() azure.APIInfo {
return azure.APIInfo{
APIVersion: apiVersion,
Method: "PUT",
URLPathFunc: searchServiceDefaultURLPath(s.ResourceGroupName, s.Name),
ResponseTypeFunc: func() interface{} {
return &CreateOrUpdateSearchServiceResponse{}
},
}
}

View File

@ -0,0 +1,19 @@
package search
import "github.com/jen20/riviera/azure"
type DeleteSearchService struct {
Name string `json:"-"`
ResourceGroupName string `json:"-"`
}
func (s DeleteSearchService) APIInfo() azure.APIInfo {
return azure.APIInfo{
APIVersion: apiVersion,
Method: "DELETE",
URLPathFunc: searchServiceDefaultURLPath(s.ResourceGroupName, s.Name),
ResponseTypeFunc: func() interface{} {
return nil
},
}
}

View File

@ -0,0 +1,33 @@
package search
import "github.com/jen20/riviera/azure"
type GetSearchServiceResponse struct {
ID *string `mapstructure:"id"`
Name string `mapstructure:"name"`
ResourceGroupName string `mapstructure:"-"`
Location string `mapstructure:"location"`
Tags map[string]*string `mapstructure:"tags"`
Sku *Sku `mapstructure:"sku"`
ReplicaCount *int `mapstructure:"replicaCount"`
PartitionCount *int `mapstructure:"partitionCount"`
Status *string `mapstructure:"status"`
StatusDetails *string `mapstructure:"statusDetails"`
ProvisioningState *string `mapstructure:"provisioningState"`
}
type GetSearchService struct {
Name string `json:"-"`
ResourceGroupName string `json:"-"`
}
func (s GetSearchService) APIInfo() azure.APIInfo {
return azure.APIInfo{
APIVersion: apiVersion,
Method: "GET",
URLPathFunc: searchServiceDefaultURLPath(s.ResourceGroupName, s.Name),
ResponseTypeFunc: func() interface{} {
return &GetSearchServiceResponse{}
},
}
}

View File

@ -0,0 +1,55 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_search_service"
sidebar_current: "docs-azurerm-resource-search-service"
description: |-
Manage a Search Service.
---
# azurerm\_search\_service
Allows you to manage an Azure Search Service
## Example Usage
```
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_search_service" "test" {
name = "acceptanceTestSearchService1"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US"
sku = "standard"
tags {
environment = "staging"
database = "test"
}
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the Search Service.
* `resource_group_name` - (Required) The name of the resource group in which to
create the Search Service.
* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.
* `sku` - (Required) Valid values are `free` and `standard`. `standard2` is also valid, but can only be used when it's enabled on the backend by Microsoft support. `free` provisions the service in shared clusters. `standard` provisions the service in dedicated clusters
* `replica_count` - (Optional) Default is 1. Valid values include 1 through 12. Valid only when `sku` is `standard`.
* `partition_count` - (Optional) Default is 1. Valid values include 1, 2, 3, 4, 6, or 12. Valid only when `sku` is `standard`.
* `tags` - (Optional) A mapping of tags to assign to the resource.
## Attributes Reference
The following attributes are exported:
* `id` - The Search Service ID.

View File

@ -112,6 +112,15 @@
</ul>
</li>
<li<%= sidebar_current(/^docs-azurerm-resource-search/) %>>
<a href="#">Search Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-azurerm-resource-search-service") %>>
<a href="/docs/providers/azurerm/r/search_service.html">azurerm_search_service</a>
</li>
</ul>
</li>
<li<%= sidebar_current(/^docs-azurerm-resource-sql/) %>>
<a href="#">SQL Resources</a>
<ul class="nav nav-visible">