forked from DrFaust92/terraform-provider-bitbucket
/
data_pipeline_oidc_config_keys.go
61 lines (49 loc) · 1.33 KB
/
data_pipeline_oidc_config_keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package bitbucket
import (
"context"
"fmt"
"io"
"log"
"net/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataPipelineOidcConfigKeys() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataReadPipelineOidcConfigKeys,
Schema: map[string]*schema.Schema{
"workspace": {
Type: schema.TypeString,
Required: true,
},
"keys": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func dataReadPipelineOidcConfigKeys(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).httpClient
workspace := d.Get("workspace").(string)
req, err := c.Get(fmt.Sprintf("2.0/workspaces/%s/pipelines-config/identity/oidc/keys.json", workspace))
if err != nil {
return diag.FromErr(err)
}
if req.StatusCode == http.StatusNotFound {
return diag.Errorf("user not found")
}
if req.StatusCode >= http.StatusInternalServerError {
return diag.Errorf("internal server error fetching user")
}
body, readerr := io.ReadAll(req.Body)
if readerr != nil {
return diag.FromErr(readerr)
}
log.Printf("[DEBUG] Pipeline Oidc Config Keys Response JSON: %v", string(body))
d.SetId(workspace)
d.Set("workspace", workspace)
d.Set("keys", string(body))
return nil
}