|
| 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 resourceKongConsumerOAuth2() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceKongConsumerOAuth2Create, |
| 15 | + ReadContext: resourceKongConsumerOAuth2Read, |
| 16 | + DeleteContext: resourceKongConsumerOAuth2Delete, |
| 17 | + UpdateContext: resourceKongConsumerOAuth2Update, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "consumer_id": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + ForceNew: false, |
| 23 | + }, |
| 24 | + "name": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + ForceNew: false, |
| 28 | + }, |
| 29 | + "client_id": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Optional: true, |
| 32 | + ForceNew: false, |
| 33 | + }, |
| 34 | + "client_secret": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + ForceNew: false, |
| 38 | + }, |
| 39 | + "hash_secret": { |
| 40 | + Type: schema.TypeBool, |
| 41 | + Optional: true, |
| 42 | + ForceNew: false, |
| 43 | + Default: false, |
| 44 | + }, |
| 45 | + "redirect_uris": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Required: true, |
| 48 | + ForceNew: false, |
| 49 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 50 | + }, |
| 51 | + "tags": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Optional: true, |
| 54 | + ForceNew: false, |
| 55 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func resourceKongConsumerOAuth2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 62 | + OAuth2CredentialRequest := &kong.Oauth2Credential{ |
| 63 | + Name: readStringPtrFromResource(d, "name"), |
| 64 | + ClientID: readStringPtrFromResource(d, "client_id"), |
| 65 | + ClientSecret: readStringPtrFromResource(d, "client_secret"), |
| 66 | + HashSecret: readBoolPtrFromResource(d, "hash_secret"), |
| 67 | + RedirectURIs: readStringArrayPtrFromResource(d, "redirect_uris"), |
| 68 | + Tags: readStringArrayPtrFromResource(d, "tags"), |
| 69 | + } |
| 70 | + |
| 71 | + consumerId := kong.String(d.Get("consumer_id").(string)) |
| 72 | + |
| 73 | + client := meta.(*config).adminClient.Oauth2Credentials |
| 74 | + oAuth2Credentials, err := client.Create(ctx, consumerId, OAuth2CredentialRequest) |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + return diag.FromErr(fmt.Errorf("failed to create oauth2 credentials: %v error: %v", OAuth2CredentialRequest, err)) |
| 78 | + } |
| 79 | + |
| 80 | + d.SetId(buildConsumerPairID(*oAuth2Credentials.ID, *consumerId)) |
| 81 | + |
| 82 | + return resourceKongConsumerOAuth2Read(ctx, d, meta) |
| 83 | +} |
| 84 | + |
| 85 | +func resourceKongConsumerOAuth2Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 86 | + id, _ := splitConsumerID(d.Id()) |
| 87 | + |
| 88 | + OAuth2CredentialRequest := &kong.Oauth2Credential{ |
| 89 | + ID: kong.String(id.ID), |
| 90 | + Name: readStringPtrFromResource(d, "name"), |
| 91 | + ClientID: readStringPtrFromResource(d, "client_id"), |
| 92 | + ClientSecret: readStringPtrFromResource(d, "client_secret"), |
| 93 | + HashSecret: readBoolPtrFromResource(d, "hash_secret"), |
| 94 | + RedirectURIs: readStringArrayPtrFromResource(d, "redirect_uris"), |
| 95 | + Tags: readStringArrayPtrFromResource(d, "tags"), |
| 96 | + } |
| 97 | + |
| 98 | + consumerId := kong.String(d.Get("consumer_id").(string)) |
| 99 | + |
| 100 | + client := meta.(*config).adminClient.Oauth2Credentials |
| 101 | + _, err := client.Update(ctx, consumerId, OAuth2CredentialRequest) |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + return diag.FromErr(fmt.Errorf("error updating kong oauth2 credentials: %s", err)) |
| 105 | + } |
| 106 | + |
| 107 | + return resourceKongConsumerOAuth2Read(ctx, d, meta) |
| 108 | +} |
| 109 | + |
| 110 | +func resourceKongConsumerOAuth2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 111 | + var diags diag.Diagnostics |
| 112 | + id, err := splitConsumerID(d.Id()) |
| 113 | + if err != nil { |
| 114 | + return diag.FromErr(err) |
| 115 | + } |
| 116 | + |
| 117 | + client := meta.(*config).adminClient.Oauth2Credentials |
| 118 | + oAuth2Credentials, err := client.Get(ctx, kong.String(id.ConsumerID), kong.String(id.ID)) |
| 119 | + |
| 120 | + if kong.IsNotFoundErr(err) { |
| 121 | + d.SetId("") |
| 122 | + } else if err != nil { |
| 123 | + return diag.FromErr(fmt.Errorf("could not find kong oauth2 credentials with id: %s error: %v", id, err)) |
| 124 | + } |
| 125 | + |
| 126 | + if oAuth2Credentials == nil { |
| 127 | + d.SetId("") |
| 128 | + } else { |
| 129 | + err = d.Set("consumer_id", oAuth2Credentials.Consumer.ID) |
| 130 | + if err != nil { |
| 131 | + return diag.FromErr(err) |
| 132 | + } |
| 133 | + err = d.Set("name", oAuth2Credentials.Name) |
| 134 | + if err != nil { |
| 135 | + return diag.FromErr(err) |
| 136 | + } |
| 137 | + err = d.Set("client_id", oAuth2Credentials.ClientID) |
| 138 | + if err != nil { |
| 139 | + return diag.FromErr(err) |
| 140 | + } |
| 141 | + err = d.Set("client_secret", oAuth2Credentials.ClientSecret) |
| 142 | + if err != nil { |
| 143 | + return diag.FromErr(err) |
| 144 | + } |
| 145 | + err = d.Set("hash_secret", oAuth2Credentials.HashSecret) |
| 146 | + if err != nil { |
| 147 | + return diag.FromErr(err) |
| 148 | + } |
| 149 | + err = d.Set("redirect_uris", oAuth2Credentials.RedirectURIs) |
| 150 | + if err != nil { |
| 151 | + return diag.FromErr(err) |
| 152 | + } |
| 153 | + err = d.Set("tags", oAuth2Credentials.Tags) |
| 154 | + if err != nil { |
| 155 | + return diag.FromErr(err) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return diags |
| 160 | +} |
| 161 | + |
| 162 | +func resourceKongConsumerOAuth2Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 163 | + var diags diag.Diagnostics |
| 164 | + id, err := splitConsumerID(d.Id()) |
| 165 | + if err != nil { |
| 166 | + return diag.FromErr(err) |
| 167 | + } |
| 168 | + client := meta.(*config).adminClient.Oauth2Credentials |
| 169 | + err = client.Delete(ctx, kong.String(id.ConsumerID), kong.String(id.ID)) |
| 170 | + |
| 171 | + if err != nil { |
| 172 | + return diag.FromErr(fmt.Errorf("could not delete kong oauth2 credentials: %v", err)) |
| 173 | + } |
| 174 | + |
| 175 | + return diags |
| 176 | +} |
0 commit comments