-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source/Resource:
azurerm_elasticsearch
(#14821)
Co-authored-by: Utkarsh Jain <[email protected]> Co-authored-by: tombuildsstuff <[email protected]> Co-authored-by: Kyriakos Oikonomakos <[email protected]>
- Loading branch information
1 parent
5feff05
commit f27ba44
Showing
92 changed files
with
7,115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
internal/services/elastic/elasticsearch_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.