diff --git a/bitbucket/data_deployment.go b/bitbucket/data_deployment.go index 9c2feb86..e1811447 100644 --- a/bitbucket/data_deployment.go +++ b/bitbucket/data_deployment.go @@ -57,11 +57,11 @@ func dataReadDeployment(ctx context.Context, d *schema.ResourceData, m interface } if res.StatusCode == http.StatusNotFound { - return diag.Errorf("user not found") + return diag.Errorf("deployment not found") } if res.StatusCode >= http.StatusInternalServerError { - return diag.Errorf("internal server error fetching user") + return diag.Errorf("internal server error fetching deployment") } var deploy Deployment diff --git a/bitbucket/data_repository.go b/bitbucket/data_repository.go new file mode 100644 index 00000000..ed92232e --- /dev/null +++ b/bitbucket/data_repository.go @@ -0,0 +1,80 @@ +package bitbucket + +import ( + "context" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + + "github.com/DrFaust92/bitbucket-go-client" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataRepository() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataReadRepository, + + Schema: map[string]*schema.Schema{ + "uuid": { + Type: schema.TypeString, + Computed: true, + }, + "slug": { + Type: schema.TypeString, + Required: true, + }, + "workspace": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func dataReadRepository(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(Clients).httpClient + + slug := d.Get("slug").(string) + workspace := d.Get("workspace").(string) + + res, err := c.Get(fmt.Sprintf("2.0/repositories/%s/%s", + workspace, + slug)) + + if err != nil { + return diag.FromErr(err) + } + + if res.StatusCode == http.StatusNotFound { + return diag.Errorf(("repository not found")) + } + + if res.StatusCode == http.StatusInternalServerError { + return diag.Errorf("internal server error fetching repository") + } + + var repository bitbucket.Repository + body, readerr := io.ReadAll(res.Body) + + if readerr != nil { + return diag.FromErr(readerr) + } + + log.Printf("[DEBUG] Deployment response raw: %s", string(body)) + + decodeerr := json.Unmarshal(body, &repository) + if decodeerr != nil { + return diag.FromErr(decodeerr) + } + + log.Printf("[DEBUG] Deployment response: %#v", repository) + + d.SetId(fmt.Sprintf("%s/%s", workspace, slug)) + d.Set("workspace", workspace) + d.Set("slug", slug) + + return nil +} diff --git a/bitbucket/provider.go b/bitbucket/provider.go index e241b1df..7a04b922 100644 --- a/bitbucket/provider.go +++ b/bitbucket/provider.go @@ -106,6 +106,7 @@ func Provider() *schema.Provider { "bitbucket_ip_ranges": dataIPRanges(), "bitbucket_pipeline_oidc_config": dataPipelineOidcConfig(), "bitbucket_pipeline_oidc_config_keys": dataPipelineOidcConfigKeys(), + "bitbucket_repository": dataRepository(), "bitbucket_user": dataUser(), "bitbucket_workspace": dataWorkspace(), "bitbucket_workspace_members": dataWorkspaceMembers(),