|
| 1 | +/* |
| 2 | +*TBD* |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "awx_notification_template" "default" { |
| 8 | + name = "notification_template-test" |
| 9 | +} |
| 10 | +``` |
| 11 | +
|
| 12 | +*/ |
| 13 | +package awx |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "log" |
| 19 | + "strconv" |
| 20 | + |
| 21 | + awx "github.com/denouche/goawx/client" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | +) |
| 25 | + |
| 26 | +func resourceNotificationTemplate() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + CreateContext: resourceNotificationTemplateCreate, |
| 29 | + ReadContext: resourceNotificationTemplateRead, |
| 30 | + UpdateContext: resourceNotificationTemplateUpdate, |
| 31 | + DeleteContext: resourceNotificationTemplateDelete, |
| 32 | + |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + }, |
| 38 | + "description": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + }, |
| 42 | + "organization_id": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + }, |
| 46 | + "notification_type": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + ExactlyOneOf: []string{ |
| 50 | + "email", "grafana", "irc", "mattermost", "pagerduty", "rocketchat", "slack", "twilio", "webhook", |
| 51 | + }, |
| 52 | + }, |
| 53 | + "notification_configuration": { |
| 54 | + Type: schema.TypeMap, |
| 55 | + Optional: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func resourceNotificationTemplateCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 62 | + var diags diag.Diagnostics |
| 63 | + client := m.(*awx.AWX) |
| 64 | + awxService := client.NotificationTemplatesService |
| 65 | + |
| 66 | + result, err := awxService.Create(map[string]interface{}{ |
| 67 | + "name": d.Get("name").(string), |
| 68 | + "description": d.Get("description").(string), |
| 69 | + "organization": d.Get("organization_id").(string), |
| 70 | + "notification_type": d.Get("notification_type").(string), |
| 71 | + "notification_configuration": d.Get("notification_configuration").(map[string]interface{}), |
| 72 | + }, map[string]string{}) |
| 73 | + if err != nil { |
| 74 | + log.Printf("Fail to Create notification_template %v", err) |
| 75 | + diags = append(diags, diag.Diagnostic{ |
| 76 | + Severity: diag.Error, |
| 77 | + Summary: "Unable to create NotificationTemplate", |
| 78 | + Detail: fmt.Sprintf("NotificationTemplate failed to create %s", err.Error()), |
| 79 | + }) |
| 80 | + return diags |
| 81 | + } |
| 82 | + |
| 83 | + d.SetId(strconv.Itoa(result.ID)) |
| 84 | + return resourceNotificationTemplateRead(ctx, d, m) |
| 85 | +} |
| 86 | + |
| 87 | +func resourceNotificationTemplateUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 88 | + var diags diag.Diagnostics |
| 89 | + client := m.(*awx.AWX) |
| 90 | + awxService := client.NotificationTemplatesService |
| 91 | + id, diags := convertStateIDToNummeric("Update NotificationTemplate", d) |
| 92 | + if diags.HasError() { |
| 93 | + return diags |
| 94 | + } |
| 95 | + |
| 96 | + params := make(map[string]string) |
| 97 | + _, err := awxService.GetById(id, params) |
| 98 | + if err != nil { |
| 99 | + return buildDiagNotFoundFail("notification_template", id, err) |
| 100 | + } |
| 101 | + |
| 102 | + _, err = awxService.Update(id, map[string]interface{}{ |
| 103 | + "name": d.Get("name").(string), |
| 104 | + "description": d.Get("description").(string), |
| 105 | + "organization": d.Get("organization_id").(string), |
| 106 | + "notification_type": d.Get("notification_type").(string), |
| 107 | + "notification_configuration": d.Get("notification_configuration").(map[string]interface{}), |
| 108 | + }, map[string]string{}) |
| 109 | + if err != nil { |
| 110 | + diags = append(diags, diag.Diagnostic{ |
| 111 | + Severity: diag.Error, |
| 112 | + Summary: "Unable to update NotificationTemplate", |
| 113 | + Detail: fmt.Sprintf("notification_template with name %s failed to update %s", d.Get("name").(string), err.Error()), |
| 114 | + }) |
| 115 | + return diags |
| 116 | + } |
| 117 | + |
| 118 | + return resourceNotificationTemplateRead(ctx, d, m) |
| 119 | +} |
| 120 | + |
| 121 | +func resourceNotificationTemplateRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 122 | + var diags diag.Diagnostics |
| 123 | + client := m.(*awx.AWX) |
| 124 | + awxService := client.NotificationTemplatesService |
| 125 | + id, diags := convertStateIDToNummeric("Read notification_template", d) |
| 126 | + if diags.HasError() { |
| 127 | + return diags |
| 128 | + } |
| 129 | + |
| 130 | + res, err := awxService.GetById(id, make(map[string]string)) |
| 131 | + if err != nil { |
| 132 | + return buildDiagNotFoundFail("notification_template", id, err) |
| 133 | + |
| 134 | + } |
| 135 | + d = setNotificationTemplateResourceData(d, res) |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func resourceNotificationTemplateDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 140 | + client := m.(*awx.AWX) |
| 141 | + awxService := client.NotificationTemplatesService |
| 142 | + id, diags := convertStateIDToNummeric(diagElementHostTitle, d) |
| 143 | + if diags.HasError() { |
| 144 | + return diags |
| 145 | + } |
| 146 | + |
| 147 | + if _, err := awxService.Delete(id); err != nil { |
| 148 | + return buildDiagDeleteFail( |
| 149 | + diagElementHostTitle, |
| 150 | + fmt.Sprintf("id %v, got %s ", |
| 151 | + id, err.Error())) |
| 152 | + } |
| 153 | + d.SetId("") |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func setNotificationTemplateResourceData(d *schema.ResourceData, r *awx.NotificationTemplate) *schema.ResourceData { |
| 158 | + d.Set("name", r.Name) |
| 159 | + d.Set("description", r.Description) |
| 160 | + d.Set("organization", r.Organization) |
| 161 | + d.Set("notification_type", r.NotificationType) |
| 162 | + d.Set("notification_configuration", r.NotificationConfiguration) |
| 163 | + d.SetId(strconv.Itoa(r.ID)) |
| 164 | + return d |
| 165 | +} |
0 commit comments