Skip to content

Commit

Permalink
Merge pull request #124 from DrFaust92/ctx-resources
Browse files Browse the repository at this point in the history
ctx for resources
  • Loading branch information
DrFaust92 authored Dec 18, 2022
2 parents e04ff96 + 4210af7 commit 543095d
Show file tree
Hide file tree
Showing 19 changed files with 387 additions and 351 deletions.
40 changes: 21 additions & 19 deletions bitbucket/resource_branching_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package bitbucket

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -36,10 +38,10 @@ type BranchType struct {

func resourceBranchingModel() *schema.Resource {
return &schema.Resource{
Create: resourceBranchingModelsPut,
Read: resourceBranchingModelsRead,
Update: resourceBranchingModelsPut,
Delete: resourceBranchingModelsDelete,
CreateWithoutTimeout: resourceBranchingModelsPut,
ReadWithoutTimeout: resourceBranchingModelsRead,
UpdateWithoutTimeout: resourceBranchingModelsPut,
DeleteWithoutTimeout: resourceBranchingModelsDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -137,15 +139,15 @@ func resourceBranchingModel() *schema.Resource {
}
}

func resourceBranchingModelsPut(d *schema.ResourceData, m interface{}) error {
func resourceBranchingModelsPut(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(Clients).httpClient
branchingModel := expandBranchingModel(d)

log.Printf("[DEBUG] Branching Model Request: %#v", branchingModel)
bytedata, err := json.Marshal(branchingModel)

if err != nil {
return err
return diag.FromErr(err)
}

branchingModelReq, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s/branching-model/settings",
Expand All @@ -154,30 +156,30 @@ func resourceBranchingModelsPut(d *schema.ResourceData, m interface{}) error {
), bytes.NewBuffer(bytedata))

if err != nil {
return err
return diag.FromErr(err)
}

body, readerr := io.ReadAll(branchingModelReq.Body)
if readerr != nil {
return readerr
return diag.FromErr(readerr)
}

decodeerr := json.Unmarshal(body, &branchingModel)
if decodeerr != nil {
return decodeerr
return diag.FromErr(decodeerr)
}

d.SetId(string(fmt.Sprintf("%s/%s", d.Get("owner").(string), d.Get("repository").(string))))

return resourceBranchingModelsRead(d, m)
return resourceBranchingModelsRead(ctx, d, m)
}

func resourceBranchingModelsRead(d *schema.ResourceData, m interface{}) error {
func resourceBranchingModelsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(Clients).httpClient

owner, repo, err := branchingModelId(d.Id())
if err != nil {
return err
return diag.FromErr(err)
}
branchingModelsReq, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/branching-model", owner, repo))

Expand All @@ -188,20 +190,20 @@ func resourceBranchingModelsRead(d *schema.ResourceData, m interface{}) error {
}

if branchingModelsReq.Body == nil {
return fmt.Errorf("error getting Branching Model (%s): empty response", d.Id())
return diag.Errorf("error getting Branching Model (%s): empty response", d.Id())
}

var branchingModel *BranchingModel
body, readerr := io.ReadAll(branchingModelsReq.Body)
if readerr != nil {
return readerr
return diag.FromErr(readerr)
}

log.Printf("[DEBUG] Branching Model Response JSON: %v", string(body))

decodeerr := json.Unmarshal(body, &branchingModel)
if decodeerr != nil {
return decodeerr
return diag.FromErr(decodeerr)
}

log.Printf("[DEBUG] Branching Model Response Decoded: %#v", branchingModel)
Expand All @@ -215,21 +217,21 @@ func resourceBranchingModelsRead(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceBranchingModelsDelete(d *schema.ResourceData, m interface{}) error {
func resourceBranchingModelsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(Clients).httpClient

owner, repo, err := branchingModelId(d.Id())
if err != nil {
return err
return diag.FromErr(err)
}

_, err = client.Put(fmt.Sprintf("2.0/repositories/%s/%s/branching-model/settings", owner, repo), nil)

if err != nil {
return err
return diag.FromErr(err)
}

return err
return diag.FromErr(err)
}

func expandBranchingModel(d *schema.ResourceData) *BranchingModel {
Expand Down
46 changes: 24 additions & 22 deletions bitbucket/resource_default_reviewers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bitbucket

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -27,10 +29,10 @@ type PaginatedReviewers struct {

func resourceDefaultReviewers() *schema.Resource {
return &schema.Resource{
Create: resourceDefaultReviewersCreate,
Read: resourceDefaultReviewersRead,
Update: resourceDefaultReviewersUpdate,
Delete: resourceDefaultReviewersDelete,
CreateWithoutTimeout: resourceDefaultReviewersCreate,
ReadWithoutTimeout: resourceDefaultReviewersRead,
UpdateWithoutTimeout: resourceDefaultReviewersUpdate,
DeleteWithoutTimeout: resourceDefaultReviewersDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -55,7 +57,7 @@ func resourceDefaultReviewers() *schema.Resource {
}
}

func resourceDefaultReviewersCreate(d *schema.ResourceData, m interface{}) error {
func resourceDefaultReviewersCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
prApi := c.ApiClient.PullrequestsApi

Expand All @@ -66,30 +68,30 @@ func resourceDefaultReviewersCreate(d *schema.ResourceData, m interface{}) error
_, reviewerResp, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernamePut(c.AuthContext, repo, userName, workspace)

if err != nil {
return err
return diag.FromErr(err)
}

if reviewerResp.StatusCode != 200 {
return fmt.Errorf("failed to create reviewer %s got code %d", userName, reviewerResp.StatusCode)
return diag.Errorf("failed to create reviewer %s got code %d", userName, reviewerResp.StatusCode)
}
}

d.SetId(fmt.Sprintf("%s/%s/reviewers", workspace, repo))
return resourceDefaultReviewersRead(d, m)
return resourceDefaultReviewersRead(ctx, d, m)
}

func resourceDefaultReviewersRead(d *schema.ResourceData, m interface{}) error {
func resourceDefaultReviewersRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(Clients).httpClient

owner, repo, err := defaultReviewersId(d.Id())
if err != nil {
return err
return diag.FromErr(err)
}
resourceURL := fmt.Sprintf("2.0/repositories/%s/%s/default-reviewers", owner, repo)

res, err := client.Get(resourceURL)
if err != nil {
return err
return diag.FromErr(err)
}

if res.StatusCode == http.StatusNotFound {
Expand All @@ -104,13 +106,13 @@ func resourceDefaultReviewersRead(d *schema.ResourceData, m interface{}) error {
for {
reviewersResponse, err := client.Get(resourceURL)
if err != nil {
return err
return diag.FromErr(err)
}

decoder := json.NewDecoder(reviewersResponse.Body)
err = decoder.Decode(&reviewers)
if err != nil {
return err
return diag.FromErr(err)
}

for _, reviewer := range reviewers.Values {
Expand All @@ -133,7 +135,7 @@ func resourceDefaultReviewersRead(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceDefaultReviewersUpdate(d *schema.ResourceData, m interface{}) error {
func resourceDefaultReviewersUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient

prApi := c.ApiClient.PullrequestsApi
Expand All @@ -151,11 +153,11 @@ func resourceDefaultReviewersUpdate(d *schema.ResourceData, m interface{}) error
_, reviewerResp, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernamePut(c.AuthContext, repo, userName, workspace)

if err != nil {
return err
return diag.FromErr(err)
}

if reviewerResp.StatusCode != 200 {
return fmt.Errorf("failed to create reviewer %s got code %d", userName, reviewerResp.StatusCode)
return diag.Errorf("failed to create reviewer %s got code %d", userName, reviewerResp.StatusCode)
}
}

Expand All @@ -164,21 +166,21 @@ func resourceDefaultReviewersUpdate(d *schema.ResourceData, m interface{}) error
reviewerResp, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernameDelete(c.AuthContext, repo, userName, workspace)

if err != nil {
return err
return diag.FromErr(err)
}

if reviewerResp.StatusCode != 204 {
return fmt.Errorf("[%d] Could not delete %s from default reviewers",
return diag.Errorf("[%d] Could not delete %s from default reviewers",
reviewerResp.StatusCode,
userName,
)
}
}

return resourceDefaultReviewersRead(d, m)
return resourceDefaultReviewersRead(ctx, d, m)
}

func resourceDefaultReviewersDelete(d *schema.ResourceData, m interface{}) error {
func resourceDefaultReviewersDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
prApi := c.ApiClient.PullrequestsApi

Expand All @@ -189,11 +191,11 @@ func resourceDefaultReviewersDelete(d *schema.ResourceData, m interface{}) error
reviewerResp, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernameDelete(c.AuthContext, repo, userName, workspace)

if err != nil {
return err
return diag.FromErr(err)
}

if reviewerResp.StatusCode != 204 {
return fmt.Errorf("[%d] Could not delete %s from default reviewer",
return diag.Errorf("[%d] Could not delete %s from default reviewer",
reviewerResp.StatusCode,
userName,
)
Expand Down
Loading

0 comments on commit 543095d

Please sign in to comment.