Skip to content

Commit

Permalink
fixed the resource code
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyansh-db committed Jul 29, 2024
1 parent 7f5de75 commit 17b0463
Showing 1 changed file with 22 additions and 59 deletions.
81 changes: 22 additions & 59 deletions settings/resource_notification_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,31 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func setState(d *schema.ResourceData) {
d.Set("slack_url", d.Get("config.0.slack.0.url"))
d.Set("microsoft_teams_url", d.Get("config.0.microsoft_teams.0.url"))
d.Set("pagerduty_integration_key", d.Get("config.0.pagerduty.0.integration_key"))
d.Set("generic_webhook_url", d.Get("config.0.generic_webhook.0.url"))
d.Set("generic_webhook_password", d.Get("config.0.generic_webhook.0.password"))
d.Set("generic_webhook_username", d.Get("config.0.generic_webhook.0.username"))
func setStruct(s *settings.NotificationDestination, readND *settings.NotificationDestination) {
switch readND.DestinationType {
case settings.DestinationTypeSlack:
readND.Config.Slack.Url = s.Config.Slack.Url
case settings.DestinationTypePagerduty:
readND.Config.Pagerduty.IntegrationKey = s.Config.Pagerduty.IntegrationKey
case settings.DestinationTypeMicrosoftTeams:
readND.Config.MicrosoftTeams.Url = s.Config.MicrosoftTeams.Url
case settings.DestinationTypeWebhook:
if readND.Config.GenericWebhook.UrlSet {
readND.Config.GenericWebhook.Url = s.Config.GenericWebhook.Url
}
if readND.Config.GenericWebhook.PasswordSet {
readND.Config.GenericWebhook.Password = s.Config.GenericWebhook.Password
}
if readND.Config.GenericWebhook.UsernameSet {
readND.Config.GenericWebhook.Username = s.Config.GenericWebhook.Username
}
}
}

type NDStruct struct {
SlackUrl string `json:"slack_url,omitempty"`
MicrosoftTeamsUrl string `json:"microsoft_teams_url,omitempty"`
PagerDutyIntegrationKey string `json:"pagerduty_integration_key,omitempty"`
GenericWebhookUrl string `json:"generic_webhook_url,omitempty"`
GenericWebhookPassword string `json:"generic_webhook_password,omitempty"`
GenericWebhookUsername string `json:"generic_webhook_username,omitempty"`
settings.NotificationDestination
}

func customDiffSlackUrl(k, old, new string, d *schema.ResourceData) bool {
return d.Get("slack_url") == new
}

func customDiffMicrosoftTeamsUrl(k, old, new string, d *schema.ResourceData) bool {
return d.Get("microsoft_teams_url") == new
}

func customDiffPagerdutyIntegrationKey(k, old, new string, d *schema.ResourceData) bool {
return d.Get("pagerduty_integration_key") == new
}

func customDiffGenericWebhookUrl(k, old, new string, d *schema.ResourceData) bool {
return d.Get("generic_webhook_url") == new
}

func customDiffGenericWebhookUsername(k, old, new string, d *schema.ResourceData) bool {
return d.Get("generic_webhook_username") == new
}

func customDiffGenericWebhookPassword(k, old, new string, d *schema.ResourceData) bool {
return d.Get("generic_webhook_password") == new
}

func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema {
// Required fields
s.SchemaPath("display_name").SetRequired()
Expand All @@ -65,23 +47,12 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab
s.SchemaPath("config", "generic_webhook", "password_set").SetComputed()
s.SchemaPath("config", "generic_webhook", "username_set").SetComputed()

s.SchemaPath("slack_url").SetComputed()
s.SchemaPath("microsoft_teams_url").SetComputed()
s.SchemaPath("pagerduty_integration_key").SetComputed()
s.SchemaPath("generic_webhook_url").SetComputed()
s.SchemaPath("generic_webhook_password").SetComputed()
s.SchemaPath("generic_webhook_username").SetComputed()

// ForceNew fields
s.SchemaPath("destination_type").SetForceNew()

// ConflictsWith fields
config_eoo := []string{"config.0.slack", "config.0.pagerduty", "config.0.microsoft_teams", "config.0.generic_webhook", "config.0.email"}
s.SchemaPath("config", "slack").SetExactlyOneOf(config_eoo)
// s.SchemaPath("config", "pagerduty").SetExactlyOneOf(config_eoo)
// s.SchemaPath("config", "microsoft_teams").SetExactlyOneOf(config_eoo)
// s.SchemaPath("config", "generic_webhook").SetExactlyOneOf(config_eoo)
// s.SchemaPath("config", "email").SetExactlyOneOf(config_eoo)

// RequiredWith fields
s.SchemaPath("config", "slack").SetRequiredWith([]string{"config.0.slack.0.url"})
Expand All @@ -90,14 +61,6 @@ func (NDStruct) CustomizeSchema(s *common.CustomizableSchema) *common.Customizab
s.SchemaPath("config", "generic_webhook").SetRequiredWith([]string{"config.0.generic_webhook.0.url"})
s.SchemaPath("config", "email").SetRequiredWith([]string{"config.0.email.0.addresses"})

// CustomSuppressDiff fields
s.SchemaPath("config", "slack", "url").SetCustomSuppressDiff(customDiffSlackUrl)
s.SchemaPath("config", "microsoft_teams", "url").SetCustomSuppressDiff(customDiffMicrosoftTeamsUrl)
s.SchemaPath("config", "pagerduty", "integration_key").SetCustomSuppressDiff(customDiffPagerdutyIntegrationKey)
s.SchemaPath("config", "generic_webhook", "url").SetCustomSuppressDiff(customDiffGenericWebhookUrl)
s.SchemaPath("config", "generic_webhook", "username").SetCustomSuppressDiff(customDiffGenericWebhookUsername)
s.SchemaPath("config", "generic_webhook", "password").SetCustomSuppressDiff(customDiffGenericWebhookPassword)

// Sensitive fields
s.SchemaPath("config", "generic_webhook", "password").SetSensitive()
s.SchemaPath("config", "generic_webhook", "username").SetSensitive()
Expand Down Expand Up @@ -127,22 +90,23 @@ func ResourceNotificationDestination() common.Resource {
return err
}
d.SetId(createdND.Id)
setState(d)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var tempND settings.NotificationDestination
common.DataToStructPointer(d, ndSchema, &tempND)

readND, err := w.NotificationDestinations.Get(ctx, settings.GetNotificationDestinationRequest{
Id: d.Id(),
})
if err != nil {
return err
}

setStruct(&tempND, readND)
return common.StructToData(readND, ndSchema, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
Expand All @@ -157,7 +121,6 @@ func ResourceNotificationDestination() common.Resource {
if err != nil {
return err
}
setState(d)
return nil
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
Expand Down

0 comments on commit 17b0463

Please sign in to comment.