|
| 1 | +/* |
| 2 | +This resource configure generic AWX settings. |
| 3 | +Please note that resource deletion only delete object from terraform state and do not reset setting to his initial value. |
| 4 | +
|
| 5 | +See available settings list here: https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/Settings/Settings_settings_update |
| 6 | +
|
| 7 | +Example Usage |
| 8 | +
|
| 9 | +```hcl |
| 10 | +resource "awx_setting" "social_auth_saml_technical_contact" { |
| 11 | + name = "SOCIAL_AUTH_SAML_TECHNICAL_CONTACT" |
| 12 | + value_json = { |
| 13 | + givenName = "Myorg" |
| 14 | + emailAddress = "test@foo.com" |
| 15 | + } |
| 16 | +} |
| 17 | +
|
| 18 | +resource "awx_setting" "social_auth_saml_sp_entity_id" { |
| 19 | + name = "SOCIAL_AUTH_SAML_SP_ENTITY_ID" |
| 20 | + value = "test" |
| 21 | +} |
| 22 | +
|
| 23 | +resource "awx_setting" "schedule_max_jobs" { |
| 24 | + name = "SCHEDULE_MAX_JOBS" |
| 25 | + value = 15 |
| 26 | +} |
| 27 | +``` |
| 28 | +
|
| 29 | +*/ |
| 30 | +package awx |
| 31 | + |
| 32 | +import ( |
| 33 | + "context" |
| 34 | + "time" |
| 35 | + |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 37 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 38 | + awx "github.com/mrcrilly/goawx/client" |
| 39 | +) |
| 40 | + |
| 41 | +func resourceSetting() *schema.Resource { |
| 42 | + return &schema.Resource{ |
| 43 | + CreateContext: resourceSettingUpdate, |
| 44 | + ReadContext: resourceSettingRead, |
| 45 | + DeleteContext: resourceSettingDelete, |
| 46 | + UpdateContext: resourceSettingUpdate, |
| 47 | + |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "name": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + Description: "Name of setting to modify", |
| 53 | + }, |
| 54 | + "value": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + Description: "Value to be modified for given setting. This field is mutually exclusive with the field `value_json`", |
| 58 | + ConflictsWith: []string{ |
| 59 | + "value_json", |
| 60 | + }, |
| 61 | + }, |
| 62 | + "value_json": { |
| 63 | + Type: schema.TypeMap, |
| 64 | + Optional: true, |
| 65 | + Description: "Value to be modified for given setting json formated. This field is mutually exclusive with the field `value`", |
| 66 | + ConflictsWith: []string{ |
| 67 | + "value", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + Importer: &schema.ResourceImporter{ |
| 72 | + State: schema.ImportStatePassthrough, |
| 73 | + }, |
| 74 | + |
| 75 | + Timeouts: &schema.ResourceTimeout{ |
| 76 | + Create: schema.DefaultTimeout(1 * time.Minute), |
| 77 | + Update: schema.DefaultTimeout(1 * time.Minute), |
| 78 | + Delete: schema.DefaultTimeout(5 * time.Minute), |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +type setting map[string]string |
| 84 | + |
| 85 | +func resourceSettingUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 86 | + client := m.(*awx.AWX) |
| 87 | + awxService := client.SettingService |
| 88 | + |
| 89 | + _, err := awxService.GetSettingsBySlug("all", make(map[string]string)) |
| 90 | + if err != nil { |
| 91 | + return buildDiagnosticsMessage( |
| 92 | + "Create: failed to fetch settings", |
| 93 | + "Failed to fetch setting, got: %s", err.Error(), |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + var value interface{} |
| 98 | + name := d.Get("name").(string) |
| 99 | + |
| 100 | + if v, ok := d.GetOk("value"); ok { |
| 101 | + value = v |
| 102 | + } else if v, ok := d.GetOk("value_json"); ok { |
| 103 | + value = v |
| 104 | + } else { |
| 105 | + return buildDiagnosticsMessage( |
| 106 | + "Wrong input value", |
| 107 | + "`value` or `value_json` need to be define, got: value: %s, value_json: %s", d.Get("value"), d.Get("value_json"), |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + payload := map[string]interface{}{ |
| 112 | + name: value, |
| 113 | + } |
| 114 | + |
| 115 | + _, err = awxService.UpdateSettings("all", payload, make(map[string]string)) |
| 116 | + if err != nil { |
| 117 | + return buildDiagnosticsMessage( |
| 118 | + "Create: setting not created", |
| 119 | + "failed to save setting data, got: %s, %s", err.Error(), value, |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + d.SetId(name) |
| 124 | + return resourceSettingRead(ctx, d, m) |
| 125 | +} |
| 126 | + |
| 127 | +func resourceSettingRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 128 | + var diags diag.Diagnostics |
| 129 | + client := m.(*awx.AWX) |
| 130 | + awxService := client.SettingService |
| 131 | + |
| 132 | + _, err := awxService.GetSettingsBySlug("all", make(map[string]string)) |
| 133 | + if err != nil { |
| 134 | + return buildDiagnosticsMessage( |
| 135 | + "Unable to fetch settings", |
| 136 | + "Unable to load settings with slug all: got %s", err.Error(), |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + d.Set("name", d.Id()) |
| 141 | + d.Set("value", d.Get("value").(string)) |
| 142 | + return diags |
| 143 | +} |
| 144 | + |
| 145 | +func resourceSettingDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 146 | + var diags diag.Diagnostics |
| 147 | + |
| 148 | + d.SetId("") |
| 149 | + return diags |
| 150 | +} |
0 commit comments