Skip to content

Commit

Permalink
New Data Source/Resource: azurerm_elasticsearch (#14821)
Browse files Browse the repository at this point in the history
Co-authored-by: Utkarsh Jain <[email protected]>
Co-authored-by: tombuildsstuff <[email protected]>
Co-authored-by: Kyriakos Oikonomakos <[email protected]>
  • Loading branch information
4 people authored May 17, 2022
1 parent 5feff05 commit f27ba44
Show file tree
Hide file tree
Showing 92 changed files with 7,115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/labeler-pull-request-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ service/dns:
service/domain-services:
- internal/services/domainservices/**/*

service/elastic:
- internal/services/elastic/**/*

service/event-grid:
- internal/services/eventgrid/**/*

Expand Down
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var services = mapOf(
"digitaltwins" to "Digital Twins",
"disks" to "Disks",
"domainservices" to "DomainServices",
"elastic" to "Elastic",
"eventgrid" to "EventGrid",
"eventhub" to "EventHub",
"firewall" to "Firewall",
Expand Down
3 changes: 3 additions & 0 deletions .teamcity/components/settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ var serviceTestConfigurationOverrides = mapOf(
// "hdinsight" is super expensive - G class VM's are not available in westus2, quota only available in westeurope currently
"hdinsight" to testConfiguration(daysOfWeek = "2,4,6", locationOverride = LocationConfiguration("westeurope", "southeastasia", "eastus2", false)),

// Elastic can't provision many in parallel
"elastic" to testConfiguration(parallelism = 1),

// HPC Cache has a 4 instance per subscription quota as of early 2021
"hpccache" to testConfiguration(parallelism = 3, daysOfWeek = "2,4,6"),

Expand Down
3 changes: 3 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
disks "github.com/hashicorp/terraform-provider-azurerm/internal/services/disks/client"
dns "github.com/hashicorp/terraform-provider-azurerm/internal/services/dns/client"
domainservices "github.com/hashicorp/terraform-provider-azurerm/internal/services/domainservices/client"
elastic "github.com/hashicorp/terraform-provider-azurerm/internal/services/elastic/client"
eventgrid "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventgrid/client"
eventhub "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub/client"
firewall "github.com/hashicorp/terraform-provider-azurerm/internal/services/firewall/client"
Expand Down Expand Up @@ -154,6 +155,7 @@ type Client struct {
Disks *disks.Client
Dns *dns.Client
DomainServices *domainservices.Client
Elastic *elastic.Client
EventGrid *eventgrid.Client
Eventhub *eventhub.Client
Firewall *firewall.Client
Expand Down Expand Up @@ -266,6 +268,7 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.Disks = disks.NewClient(o)
client.Dns = dns.NewClient(o)
client.DomainServices = domainservices.NewClient(o)
client.Elastic = elastic.NewClient(o)
client.EventGrid = eventgrid.NewClient(o)
client.Eventhub = eventhub.NewClient(o)
client.Firewall = firewall.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/disks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dns"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/domainservices"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elastic"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/eventgrid"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/firewall"
Expand Down Expand Up @@ -171,6 +172,7 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration {
digitaltwins.Registration{},
dns.Registration{},
domainservices.Registration{},
elastic.Registration{},
eventgrid.Registration{},
eventhub.Registration{},
firewall.Registration{},
Expand Down
25 changes: 25 additions & 0 deletions internal/services/elastic/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package client

import (
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elastic/sdk/2020-07-01/monitorsresource"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elastic/sdk/2020-07-01/rules"
)

type Client struct {
MonitorClient *monitorsresource.MonitorsResourceClient
TagRuleClient *rules.RulesClient
}

func NewClient(o *common.ClientOptions) *Client {
monitorClient := monitorsresource.NewMonitorsResourceClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&monitorClient.Client, o.ResourceManagerAuthorizer)

tagRuleClient := rules.NewRulesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&tagRuleClient.Client, o.ResourceManagerAuthorizer)

return &Client{
MonitorClient: &monitorClient,
TagRuleClient: &tagRuleClient,
}
}
204 changes: 204 additions & 0 deletions internal/services/elastic/elasticsearch_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package elastic

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elastic/sdk/2020-07-01/monitorsresource"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elastic/sdk/2020-07-01/rules"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elastic/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

func dataSourceElasticsearch() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceElasticsearchRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
// Required
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ElasticsearchName,
},

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

// Computed
"location": commonschema.LocationComputed(),

"sku_name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"elastic_cloud_email_address": {
Type: pluginsdk.TypeString,
Computed: true,
},

"monitoring_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"logs": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"filtering_tag": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"value": {
Type: pluginsdk.TypeString,
Computed: true,
},
"action": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"send_azuread_logs": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"send_activity_logs": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"send_subscription_logs": {
Type: pluginsdk.TypeBool,
Computed: true,
},
},
},
},

"tags": commonschema.TagsDataSource(),

"elastic_cloud_deployment_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"elastic_cloud_sso_default_url": {
Type: pluginsdk.TypeString,
Computed: true,
},
"elastic_cloud_user_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"elasticsearch_service_url": {
Type: pluginsdk.TypeString,
Computed: true,
},
"kibana_service_url": {
Type: pluginsdk.TypeString,
Computed: true,
},
"kibana_sso_uri": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}

func dataSourceElasticsearchRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Elastic.MonitorClient
logsClient := meta.(*clients.Client).Elastic.TagRuleClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := monitorsresource.NewMonitorID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.MonitorsGet(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

tagRuleId := rules.NewTagRuleID(id.SubscriptionId, id.ResourceGroupName, id.MonitorName, "default")
rulesResp, err := logsClient.TagRulesGet(ctx, tagRuleId)
if err != nil {
if !response.WasNotFound(rulesResp.HttpResponse) {
return fmt.Errorf("retrieving logs for %s: %+v", id, err)
}
}

d.Set("name", id.MonitorName)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))

if props := model.Properties; props != nil {
monitoringEnabled := false
if props.MonitoringStatus != nil {
monitoringEnabled = *props.MonitoringStatus == monitorsresource.MonitoringStatusEnabled
}
d.Set("monitoring_enabled", monitoringEnabled)

if elastic := props.ElasticProperties; elastic != nil {
if elastic.ElasticCloudDeployment != nil {
// AzureSubscriptionId is the same as the subscription deployed into, so no point exposing it
// ElasticsearchRegion is `{Cloud}-{Region}` - so the same as location/not worth exposing for now?
d.Set("elastic_cloud_deployment_id", elastic.ElasticCloudDeployment.DeploymentId)
d.Set("elasticsearch_service_url", elastic.ElasticCloudDeployment.ElasticsearchServiceUrl)
d.Set("kibana_service_url", elastic.ElasticCloudDeployment.KibanaServiceUrl)
d.Set("kibana_sso_uri", elastic.ElasticCloudDeployment.KibanaSsoUrl)
}
if elastic.ElasticCloudUser != nil {
d.Set("elastic_cloud_user_id", elastic.ElasticCloudUser.Id)
d.Set("elastic_cloud_email_address", elastic.ElasticCloudUser.EmailAddress)
d.Set("elastic_cloud_sso_default_url", elastic.ElasticCloudUser.ElasticCloudSsoDefaultUrl)
}
}
}

skuName := ""
if model.Sku != nil {
skuName = model.Sku.Name
}
d.Set("sku_name", skuName)

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

if err := d.Set("logs", flattenTagRule(rulesResp.Model)); err != nil {
return fmt.Errorf("setting `logs`: %+v", err)
}

d.SetId(id.ID())

return nil
}
46 changes: 46 additions & 0 deletions internal/services/elastic/elasticsearch_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package elastic_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type ElasticsearchDataSourceTest struct{}

func TestAccElasticsearchDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_elastic_cloud_elasticsearch", "test")
r := ElasticsearchDataSourceTest{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("elastic_cloud_email_address").Exists(),
check.That(data.ResourceName).Key("location").Exists(),
check.That(data.ResourceName).Key("sku_name").Exists(),
check.That(data.ResourceName).Key("monitoring_enabled").Exists(),
check.That(data.ResourceName).Key("elastic_cloud_deployment_id").Exists(),
check.That(data.ResourceName).Key("elastic_cloud_sso_default_url").Exists(),
check.That(data.ResourceName).Key("elastic_cloud_user_id").Exists(),
check.That(data.ResourceName).Key("elasticsearch_service_url").Exists(),
check.That(data.ResourceName).Key("kibana_service_url").Exists(),
check.That(data.ResourceName).Key("kibana_sso_uri").Exists(),
),
},
})
}

func (ElasticsearchDataSourceTest) basic(data acceptance.TestData) string {
template := ElasticsearchResourceTest{}.basic(data)
return fmt.Sprintf(`
%s
data "azurerm_elastic_cloud_elasticsearch" "test" {
name = azurerm_elastic_cloud_elasticsearch.test.name
resource_group_name = azurerm_elastic_cloud_elasticsearch.test.resource_group_name
}
`, template)
}
Loading

0 comments on commit f27ba44

Please sign in to comment.