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

New Data Source/Resource: azurerm_elasticsearch #14821

Merged
merged 36 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
eb36a78
commit 1
utkarshjain1508 Dec 30, 2021
38a3b56
Monitor CRUD 1
utkarshjain1508 Dec 31, 2021
f05ac7f
Acceptance test 1
utkarshjain1508 Jan 1, 2022
5862f04
Acceptance tests working
utkarshjain1508 Jan 2, 2022
6515efb
Adding documentation and legacysdk
utkarshjain1508 Jan 5, 2022
80bf43b
Resolving Pr tests 1
utkarshjain1508 Jan 6, 2022
e953200
Resolving Pr tests 2
utkarshjain1508 Jan 6, 2022
1849d5b
Resolving comments and fixing commit checks
utkarshjain1508 Jan 18, 2022
1ee02bb
Fixing lint errors
utkarshjain1508 Jan 24, 2022
25043c6
resolving PR comments
utkarshjain1508 Jan 27, 2022
14b9922
Resolving review comments
utkarshjain1508 Jan 31, 2022
6726b8e
Fixing github checks
utkarshjain1508 Feb 8, 2022
6ae06f4
elastic: refactoring the resources to use commonschema/splitting the …
tombuildsstuff Feb 28, 2022
d4d3ee3
elastic: embedding the Embedded SDK
tombuildsstuff Feb 28, 2022
ec6e4af
refactor: refactoring to use the embedded sdk
tombuildsstuff Feb 28, 2022
11a034c
refactor: removing the legacysdk
tombuildsstuff Feb 28, 2022
0d0b8ba
refactor: adding the service registration with a label
tombuildsstuff Feb 28, 2022
4ad99c0
r/elastic_monitor: updating to use sku_name
tombuildsstuff Feb 28, 2022
a91cc40
r/elastic_monitor: the computed fields should only be computed
tombuildsstuff Feb 28, 2022
b073888
r/elastic_monitor: moving fields to the top-level
tombuildsstuff Feb 28, 2022
3f96e26
r/elastic_monitor: updating the tests
tombuildsstuff Feb 28, 2022
22f51b5
r/elastic_monitor: adding the `logs` block to the resourc
tombuildsstuff Feb 28, 2022
2f0319f
r/elastic_monitor: refactoring to use a smaller name for test purposes
tombuildsstuff Mar 1, 2022
c1ce964
teamcity: limiting elastic to 1 in parallel
tombuildsstuff Mar 1, 2022
5979f5d
refactor: renaming `elastic_monitor` to `elastic_stack` to match the …
tombuildsstuff Mar 1, 2022
da43d1b
New Data Source: `azurerm_elastic_stack`
tombuildsstuff Mar 1, 2022
4938c00
elastic: adding docs for the Logs block
tombuildsstuff Mar 1, 2022
70c164f
d/elastic_stack: removing MaxItems since this isn't valid in the Data…
tombuildsstuff Mar 1, 2022
83063c9
`make generate`
tombuildsstuff Mar 2, 2022
10a0e81
elastic: updating the embedded sdk
tombuildsstuff Mar 14, 2022
b6c5fc7
fix formatting
Apr 8, 2022
7fb9d2b
use a valid email for testing
Apr 11, 2022
3657a3f
Changing name
utkarshjain1508 Apr 20, 2022
9533ae6
Fixing acceptance tests
utkarshjain1508 Apr 26, 2022
6d98ca9
call SetID in the elasticsearch datasource
May 13, 2022
54f0268
Changinf name to azurerm_elastic_cloud_elasticsearch
utkarshjain1508 May 16, 2022
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
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 @@ -38,6 +38,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 @@ -54,6 +54,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 @@ -43,6 +43,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 @@ -152,6 +153,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 @@ -263,6 +265,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 @@ -39,6 +39,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 @@ -169,6 +170,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