|
| 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 = <<EOF |
| 13 | + { |
| 14 | + "givenName": "Myorg", |
| 15 | + "emailAddress": "test@foo.com" |
| 16 | + } |
| 17 | + EOF |
| 18 | +} |
| 19 | +
|
| 20 | +resource "awx_setting" "social_auth_saml_sp_entity_id" { |
| 21 | + name = "SOCIAL_AUTH_SAML_SP_ENTITY_ID" |
| 22 | + value = "test" |
| 23 | +} |
| 24 | +
|
| 25 | +resource "awx_setting" "schedule_max_jobs" { |
| 26 | + name = "SCHEDULE_MAX_JOBS" |
| 27 | + value = 15 |
| 28 | +} |
| 29 | +
|
| 30 | +resource "awx_setting" "remote_host_headers" { |
| 31 | + name = "REMOTE_HOST_HEADERS" |
| 32 | + value = <<EOF |
| 33 | + [ |
| 34 | + "HTTP_X_FORWARDED_FOR", |
| 35 | + "REMOTE_ADDR", |
| 36 | + "REMOTE_HOST" |
| 37 | + ] |
| 38 | + EOF |
| 39 | +} |
| 40 | +``` |
| 41 | +
|
| 42 | +*/ |
| 43 | +package awx |
| 44 | + |
| 45 | +import ( |
| 46 | + "context" |
| 47 | + "encoding/json" |
| 48 | + "time" |
| 49 | + |
| 50 | + awx "github.com/denouche/goawx/client" |
| 51 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 52 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 53 | +) |
| 54 | + |
| 55 | +func resourceSetting() *schema.Resource { |
| 56 | + return &schema.Resource{ |
| 57 | + CreateContext: resourceSettingUpdate, |
| 58 | + ReadContext: resourceSettingRead, |
| 59 | + DeleteContext: resourceSettingDelete, |
| 60 | + UpdateContext: resourceSettingUpdate, |
| 61 | + |
| 62 | + Schema: map[string]*schema.Schema{ |
| 63 | + "name": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Required: true, |
| 66 | + Description: "Name of setting to modify", |
| 67 | + }, |
| 68 | + "value": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Required: true, |
| 71 | + Description: "Value to be modified for given setting.", |
| 72 | + }, |
| 73 | + }, |
| 74 | + Importer: &schema.ResourceImporter{ |
| 75 | + State: schema.ImportStatePassthrough, |
| 76 | + }, |
| 77 | + |
| 78 | + Timeouts: &schema.ResourceTimeout{ |
| 79 | + Create: schema.DefaultTimeout(1 * time.Minute), |
| 80 | + Update: schema.DefaultTimeout(1 * time.Minute), |
| 81 | + Delete: schema.DefaultTimeout(5 * time.Minute), |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +type setting map[string]string |
| 87 | + |
| 88 | +func resourceSettingUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 89 | + client := m.(*awx.AWX) |
| 90 | + awxService := client.SettingService |
| 91 | + |
| 92 | + _, err := awxService.GetSettingsBySlug("all", make(map[string]string)) |
| 93 | + if err != nil { |
| 94 | + return buildDiagnosticsMessage( |
| 95 | + "Create: failed to fetch settings", |
| 96 | + "Failed to fetch setting, got: %s", err.Error(), |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + var map_decoded map[string]interface{} |
| 101 | + var array_decoded []interface{} |
| 102 | + var formatted_value interface{} |
| 103 | + |
| 104 | + name := d.Get("name").(string) |
| 105 | + value := d.Get("value").(string) |
| 106 | + |
| 107 | + // Attempt to unmarshall string into a map |
| 108 | + err = json.Unmarshal([]byte(value), &map_decoded) |
| 109 | + |
| 110 | + if err != nil { |
| 111 | + // Attempt to unmarshall string into an array |
| 112 | + err = json.Unmarshal([]byte(value), &array_decoded) |
| 113 | + |
| 114 | + if err != nil { |
| 115 | + formatted_value = value |
| 116 | + } else { |
| 117 | + formatted_value = array_decoded |
| 118 | + } |
| 119 | + } else { |
| 120 | + formatted_value = map_decoded |
| 121 | + } |
| 122 | + |
| 123 | + payload := map[string]interface{}{ |
| 124 | + name: formatted_value, |
| 125 | + } |
| 126 | + |
| 127 | + _, err = awxService.UpdateSettings("all", payload, make(map[string]string)) |
| 128 | + if err != nil { |
| 129 | + return buildDiagnosticsMessage( |
| 130 | + "Create: setting not created", |
| 131 | + "failed to save setting data, got: %s, %s", err.Error(), value, |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + d.SetId(name) |
| 136 | + return resourceSettingRead(ctx, d, m) |
| 137 | +} |
| 138 | + |
| 139 | +func resourceSettingRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 140 | + var diags diag.Diagnostics |
| 141 | + client := m.(*awx.AWX) |
| 142 | + awxService := client.SettingService |
| 143 | + |
| 144 | + _, err := awxService.GetSettingsBySlug("all", make(map[string]string)) |
| 145 | + if err != nil { |
| 146 | + return buildDiagnosticsMessage( |
| 147 | + "Unable to fetch settings", |
| 148 | + "Unable to load settings with slug all: got %s", err.Error(), |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + d.Set("name", d.Id()) |
| 153 | + d.Set("value", d.Get("value").(string)) |
| 154 | + return diags |
| 155 | +} |
| 156 | + |
| 157 | +func resourceSettingDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 158 | + var diags diag.Diagnostics |
| 159 | + |
| 160 | + d.SetId("") |
| 161 | + return diags |
| 162 | +} |
0 commit comments