|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/ovh/terraform-provider-ovh/ovh/helpers" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceMeApiOauth2Client() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + ReadContext: dataSourceMeApiOauth2ClientRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "callback_urls": { |
| 18 | + Type: schema.TypeList, |
| 19 | + Description: "Callback URLs of the applications using this oauth2 client. Required if using the AUTHORIZATION_CODE flow.", |
| 20 | + Elem: &schema.Schema{ |
| 21 | + Type: schema.TypeString, |
| 22 | + }, |
| 23 | + Computed: true, |
| 24 | + }, |
| 25 | + "client_id": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Description: "Client ID for the oauth2 client, generated during the resource creation.", |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "client_secret": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "Secret for the oauth2 client, generated during the oauth2 client creation. Can be specified in the data resource.", |
| 33 | + Optional: true, |
| 34 | + Sensitive: true, |
| 35 | + }, |
| 36 | + "description": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "A description of your oauth2 client.", |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "identity": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "URN that will allow you to associate this oauth2 client with an access policy.", |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "flow": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "OAuth2 flow type implemented for this oauth2 client. Can be either AUTHORIZATION_CODE or CLIENT_CREDENTIALS", |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "name": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func dataSourceMeApiOauth2ClientRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 60 | + config := meta.(*Config) |
| 61 | + |
| 62 | + serviceAccount := &ApiOauth2ClientReadResponse{} |
| 63 | + |
| 64 | + // Query the oauth2 client using its client ID |
| 65 | + endpoint := fmt.Sprintf("/me/api/oauth2/client/%s", d.Get("client_id").(string)) |
| 66 | + if err := config.OVHClient.GetWithContext(ctx, endpoint, serviceAccount); err != nil { |
| 67 | + return diag.FromErr(helpers.CheckDeleted(d, err, endpoint)) |
| 68 | + } |
| 69 | + |
| 70 | + log.Printf("[DEBUG] Read oauth2 client: %s", endpoint) |
| 71 | + |
| 72 | + // Populate the state with the response body parameters |
| 73 | + d.SetId(serviceAccount.ClientId) |
| 74 | + d.Set("callback_urls", serviceAccount.CallbackUrls) |
| 75 | + d.Set("client_id", serviceAccount.ClientId) |
| 76 | + d.Set("description", serviceAccount.Description) |
| 77 | + d.Set("flow", serviceAccount.Flow) |
| 78 | + d.Set("identity", serviceAccount.Identity) |
| 79 | + d.Set("name", serviceAccount.Name) |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
0 commit comments