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

datadog: refactoring to use hashicorp/go-azure-sdk #20311

Merged
merged 11 commits into from
Feb 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
aadb2c_v2021_04_01_preview "github.com/hashicorp/go-azure-sdk/resource-manager/aadb2c/2021-04-01-preview"
analysisservices_v2017_08_01 "github.com/hashicorp/go-azure-sdk/resource-manager/analysisservices/2017-08-01"
azurestackhci_v2022_12_01 "github.com/hashicorp/go-azure-sdk/resource-manager/azurestackhci/2022-12-01"
datadog_v2021_03_01 "github.com/hashicorp/go-azure-sdk/resource-manager/datadog/2021-03-01"
dns_v2018_05_01 "github.com/hashicorp/go-azure-sdk/resource-manager/dns/2018-05-01"
fluidrelay_2022_05_26 "github.com/hashicorp/go-azure-sdk/resource-manager/fluidrelay/2022-05-26"
nginx2 "github.com/hashicorp/go-azure-sdk/resource-manager/nginx/2022-08-01"
Expand Down Expand Up @@ -162,7 +163,7 @@ type Client struct {
DatabaseMigration *datamigration.Client
DataBricks *databricks.Client
DataboxEdge *databoxedge.Client
Datadog *datadog.Client
Datadog *datadog_v2021_03_01.Client
DataFactory *datafactory.Client
DataProtection *dataprotection.Client
DataShare *datashare.Client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/datadog/mgmt/2021-03-01/datadog" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/datadog/2021-03-01/monitorsresource"
"github.com/hashicorp/go-azure-sdk/resource-manager/datadog/2021-03-01/singlesignon"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datadog/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datadog/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

// @tombuildsstuff: in 4.0 consider inlining this within the `azurerm_datadog_monitors` resource
// since this appears to be a 1:1 with it (given the name defaults to `default`)

func resourceDatadogSingleSignOnConfigurations() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceDatadogSingleSignOnConfigurationsCreateorUpdate,
Create: resourceDatadogSingleSignOnConfigurationsCreate,
Read: resourceDatadogSingleSignOnConfigurationsRead,
Update: resourceDatadogSingleSignOnConfigurationsCreateorUpdate,
Update: resourceDatadogSingleSignOnConfigurationsUpdate,
Delete: resourceDatadogSingleSignOnConfigurationsDelete,

Timeouts: &pluginsdk.ResourceTimeout{
Expand All @@ -29,7 +36,7 @@ func resourceDatadogSingleSignOnConfigurations() *pluginsdk.Resource {
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.DatadogSingleSignOnConfigurationsID(id)
_, err := singlesignon.ParseSingleSignOnConfigurationID(id)
return err
}),

Expand All @@ -38,7 +45,7 @@ func resourceDatadogSingleSignOnConfigurations() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DatadogMonitorID,
ValidateFunc: monitorsresource.ValidateMonitorID,
},

"name": {
Expand All @@ -56,6 +63,12 @@ func resourceDatadogSingleSignOnConfigurations() *pluginsdk.Resource {
"single_sign_on_enabled": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
// @tombuildsstuff: other options are available, but the Create handles this as a boolean for now
// should the field be a boolean? one to consider for 4.0 when this resource is inlined
string(singlesignon.SingleSignOnStatesEnable),
string(singlesignon.SingleSignOnStatesDisable),
}, false),
},

"login_url": {
Expand All @@ -66,120 +79,121 @@ func resourceDatadogSingleSignOnConfigurations() *pluginsdk.Resource {
}
}

func resourceDatadogSingleSignOnConfigurationsCreateorUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datadog.SingleSignOnConfigurationsClient
func resourceDatadogSingleSignOnConfigurationsCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datadog.SingleSignOn
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

datadogMonitorId := d.Get("datadog_monitor_id").(string)
configurationName := d.Get("name").(string)
enterpriseAppID := d.Get("enterprise_application_id").(string)
id, err := parse.DatadogMonitorID(datadogMonitorId)
monitorId, err := monitorsresource.ParseMonitorID(d.Get("datadog_monitor_id").(string))
if err != nil {
return err
}

ssoId := parse.NewDatadogSingleSignOnConfigurationsID(id.SubscriptionId, id.ResourceGroup, id.MonitorName, configurationName).ID()

existing, err := client.Get(ctx, id.ResourceGroup, id.MonitorName, configurationName)
id := singlesignon.NewSingleSignOnConfigurationID(monitorId.SubscriptionId, monitorId.ResourceGroupName, monitorId.MonitorName, d.Get("name").(string))
existing, err := client.ConfigurationsGet(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing Datadog Monitor %q (Resource Group %q): %+v", id.ResourceGroup, id.MonitorName, err)
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}

singleSignOnState := datadog.SingleSignOnStatesEnable
if d.Get("single_sign_on_enabled").(string) == "Disable" {
singleSignOnState = datadog.SingleSignOnStatesDisable
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_datadog_monitor_sso_configuration", id.ID())
}

body := datadog.SingleSignOnResource{
Properties: &datadog.SingleSignOnProperties{
SingleSignOnState: singleSignOnState,
EnterpriseAppID: utils.String(enterpriseAppID),
payload := singlesignon.DatadogSingleSignOnResource{
Properties: &singlesignon.DatadogSingleSignOnProperties{
SingleSignOnState: pointer.To(singlesignon.SingleSignOnStates(d.Get("single_sign_on_enabled").(string))),
EnterpriseAppId: utils.String(d.Get("enterprise_application_id").(string)),
},
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.MonitorName, configurationName, &body)
if err != nil {
return fmt.Errorf("configuring SingleSignOn on Datadog Monitor %q (Resource Group %q): %+v", id.MonitorName, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on configuring SingleSignOn on Datadog Monitor %s: %+v", id, err)
if err := client.ConfigurationsCreateOrUpdateThenPoll(ctx, id, payload); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

d.SetId(ssoId)
d.SetId(id.ID())
return resourceDatadogSingleSignOnConfigurationsRead(d, meta)
}

func resourceDatadogSingleSignOnConfigurationsRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datadog.SingleSignOnConfigurationsClient
client := meta.(*clients.Client).Datadog.SingleSignOn
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DatadogSingleSignOnConfigurationsID(d.Id())
id, err := singlesignon.ParseSingleSignOnConfigurationID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.MonitorName, id.SingleSignOnConfigurationName)
resp, err := client.ConfigurationsGet(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Datadog monitor %q does not exist - removing from state", d.Id())
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s does not exist - removing from state", *id)
d.SetId("")
return nil
}
}
monitorId := parse.NewDatadogMonitorID(id.SubscriptionId, id.ResourceGroup, id.MonitorName)
d.Set("datadog_monitor_id", monitorId.ID())
d.Set("name", id.SingleSignOnConfigurationName)

if props := resp.Properties; props != nil {
d.Set("single_sign_on_enabled", props.SingleSignOnState)
d.Set("login_url", props.SingleSignOnURL)
d.Set("enterprise_application_id", props.EnterpriseAppID)
d.Set("name", id.SingleSignOnConfigurationName)
d.Set("datadog_monitor_id", monitorsresource.NewMonitorID(id.SubscriptionId, id.ResourceGroupName, id.MonitorName).ID())

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
// per the create func
singleSignOnEnabled := props.SingleSignOnState != nil && *props.SingleSignOnState == singlesignon.SingleSignOnStatesEnable
d.Set("single_sign_on_enabled", singleSignOnEnabled)
d.Set("login_url", props.SingleSignOnUrl)
d.Set("enterprise_application_id", props.EnterpriseAppId)
}
}

return nil
}

func resourceDatadogSingleSignOnConfigurationsDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datadog.SingleSignOnConfigurationsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
func resourceDatadogSingleSignOnConfigurationsUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datadog.SingleSignOn
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DatadogSingleSignOnConfigurationsID(d.Id())
id, err := singlesignon.ParseSingleSignOnConfigurationID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.MonitorName, id.SingleSignOnConfigurationName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Datadog monitor %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
payload := singlesignon.DatadogSingleSignOnResource{
Properties: &singlesignon.DatadogSingleSignOnProperties{
SingleSignOnState: pointer.To(singlesignon.SingleSignOnStates(d.Get("single_sign_on_enabled").(string))),
EnterpriseAppId: utils.String(d.Get("enterprise_application_id").(string)),
},
}

d.Set("enterprise_application_id", nil)
enterpriseAppID := d.Get("enterprise_application_id").(string)

body := datadog.SingleSignOnResource{
Properties: &datadog.SingleSignOnProperties{
SingleSignOnState: datadog.SingleSignOnStatesDisable,
EnterpriseAppID: utils.String(enterpriseAppID),
},
if err := client.ConfigurationsCreateOrUpdateThenPoll(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.MonitorName, id.SingleSignOnConfigurationName, &body)
return resourceDatadogSingleSignOnConfigurationsRead(d, meta)
}

func resourceDatadogSingleSignOnConfigurationsDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datadog.SingleSignOn
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := singlesignon.ParseSingleSignOnConfigurationID(d.Id())
if err != nil {
return fmt.Errorf("removing SingleSignOnConfiguration on Datadog Monitor %q (Resource Group %q): %+v", id.MonitorName, id.ResourceGroup, err)
return err
}

// SingleSignOnConfigurations can't be removed, but can be disabled/reset, which is what we do here
payload := singlesignon.DatadogSingleSignOnResource{
Properties: &singlesignon.DatadogSingleSignOnProperties{
SingleSignOnState: pointer.To(singlesignon.SingleSignOnStatesDisable),
EnterpriseAppId: utils.String(""),
},
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on removing SingleSignOnConfiguration on Datadog Monitor %s: %+v", id, err)
if err := client.ConfigurationsCreateOrUpdateThenPoll(ctx, *id, payload); err != nil {
return fmt.Errorf("removing %s: %+v", id, err)
}

return nil
Expand Down
Loading