Skip to content

Commit

Permalink
Add repository data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Desroches committed Jun 26, 2024
1 parent 8de67e9 commit 372588f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bitbucket/data_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions bitbucket/data_repository.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 372588f

Please sign in to comment.