|
| 1 | +package kong |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/kong/go-kong/kong" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceKongConsumerBasicAuth() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceKongConsumerBasicAuthCreate, |
| 15 | + ReadContext: resourceKongConsumerBasicAuthRead, |
| 16 | + DeleteContext: resourceKongConsumerBasicAuthDelete, |
| 17 | + UpdateContext: resourceKongConsumerBasicAuthUpdate, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "consumer_id": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + ForceNew: false, |
| 23 | + }, |
| 24 | + "username": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + ForceNew: false, |
| 28 | + }, |
| 29 | + "password": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: false, |
| 33 | + }, |
| 34 | + "tags": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Optional: true, |
| 37 | + ForceNew: false, |
| 38 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func resourceKongConsumerBasicAuthCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 45 | + BasicAuthRequest := &kong.BasicAuth{ |
| 46 | + Username: kong.String(d.Get("username").(string)), |
| 47 | + Password: kong.String(d.Get("password").(string)), |
| 48 | + Tags: readStringArrayPtrFromResource(d, "tags"), |
| 49 | + } |
| 50 | + |
| 51 | + consumerId := kong.String(d.Get("consumer_id").(string)) |
| 52 | + |
| 53 | + client := meta.(*config).adminClient.BasicAuths |
| 54 | + basicAuth, err := client.Create(ctx, consumerId, BasicAuthRequest) |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + return diag.FromErr(fmt.Errorf("failed to create kong basic auth: %v error: %v", BasicAuthRequest, err)) |
| 58 | + } |
| 59 | + |
| 60 | + d.SetId(buildConsumerPairID(*basicAuth.ID, *consumerId)) |
| 61 | + |
| 62 | + return resourceKongConsumerBasicAuthRead(ctx, d, meta) |
| 63 | +} |
| 64 | + |
| 65 | +func resourceKongConsumerBasicAuthUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 66 | + id, err := splitConsumerID(d.Id()) |
| 67 | + |
| 68 | + BasicAuthRequest := &kong.BasicAuth{ |
| 69 | + ID: kong.String(id.ID), |
| 70 | + Username: kong.String(d.Get("username").(string)), |
| 71 | + Password: kong.String(d.Get("password").(string)), |
| 72 | + Tags: readStringArrayPtrFromResource(d, "tags"), |
| 73 | + } |
| 74 | + |
| 75 | + consumerId := kong.String(d.Get("consumer_id").(string)) |
| 76 | + |
| 77 | + client := meta.(*config).adminClient.BasicAuths |
| 78 | + _, err = client.Update(ctx, consumerId, BasicAuthRequest) |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + return diag.FromErr(fmt.Errorf("error updating kong basic auth: %s", err)) |
| 82 | + } |
| 83 | + |
| 84 | + return resourceKongConsumerBasicAuthRead(ctx, d, meta) |
| 85 | +} |
| 86 | + |
| 87 | +func resourceKongConsumerBasicAuthRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 88 | + var diags diag.Diagnostics |
| 89 | + id, err := splitConsumerID(d.Id()) |
| 90 | + if err != nil { |
| 91 | + return diag.FromErr(err) |
| 92 | + } |
| 93 | + |
| 94 | + client := meta.(*config).adminClient.BasicAuths |
| 95 | + basicAuth, err := client.Get(ctx, kong.String(id.ConsumerID), kong.String(id.ID)) |
| 96 | + |
| 97 | + if kong.IsNotFoundErr(err) { |
| 98 | + d.SetId("") |
| 99 | + } else if err != nil { |
| 100 | + return diag.FromErr(fmt.Errorf("could not find kong ACLGroup with id: %s error: %v", id, err)) |
| 101 | + } |
| 102 | + |
| 103 | + if basicAuth == nil { |
| 104 | + d.SetId("") |
| 105 | + } else { |
| 106 | + err = d.Set("consumer_id", basicAuth.Consumer.ID) |
| 107 | + if err != nil { |
| 108 | + return diag.FromErr(err) |
| 109 | + } |
| 110 | + err = d.Set("username", basicAuth.Username) |
| 111 | + if err != nil { |
| 112 | + return diag.FromErr(err) |
| 113 | + } |
| 114 | + err = d.Set("tags", basicAuth.Tags) |
| 115 | + if err != nil { |
| 116 | + return diag.FromErr(err) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return diags |
| 121 | +} |
| 122 | + |
| 123 | +func resourceKongConsumerBasicAuthDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 124 | + var diags diag.Diagnostics |
| 125 | + id, err := splitConsumerID(d.Id()) |
| 126 | + if err != nil { |
| 127 | + return diag.FromErr(err) |
| 128 | + } |
| 129 | + client := meta.(*config).adminClient.BasicAuths |
| 130 | + err = client.Delete(ctx, kong.String(id.ConsumerID), kong.String(id.ID)) |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + return diag.FromErr(fmt.Errorf("could not delete kong basic auth: %v", err)) |
| 134 | + } |
| 135 | + |
| 136 | + return diags |
| 137 | +} |
0 commit comments