Skip to content

Commit

Permalink
added a hasher to modify access token
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsz-rb committed Sep 3, 2024
1 parent 307d3bc commit 32838f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
13 changes: 10 additions & 3 deletions rollbar/resource_project_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ package rollbar

import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/rollbar/terraform-provider-rollbar/client"
"github.com/rs/zerolog/log"
)

func getMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}

func resourceProjectAccessToken() *schema.Resource {
return &schema.Resource{
CreateContext: resourceProjectAccessTokenCreate,
Expand Down Expand Up @@ -157,7 +164,7 @@ func resourceProjectAccessTokenCreate(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
d.SetId(getMD5Hash(pat.AccessToken))
mustSet(d, "access_token", pat.AccessToken)
return resourceProjectAccessTokenRead(ctx, d, m)
}
Expand Down Expand Up @@ -260,6 +267,6 @@ func resourceProjectAccessTokenImporter(_ context.Context, d *schema.ResourceDat
Send()
mustSet(d, "project_id", projectID)
mustSet(d, "access_token", accessToken)
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
d.SetId(getMD5Hash(accessToken))
return []*schema.ResourceData{d}, nil
}
12 changes: 6 additions & 6 deletions rollbar/test1/resource_project_access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ func (s *AccSuite) TestAccTokenDeleteOnAPIBeforeApply() {
// properties.
func (s *AccSuite) checkProjectAccessToken(resourceName string) resource.TestCheckFunc {
return func(ts *terraform.State) error {
accessToken, err := s.getResourceIDString(ts, resourceName)
if err != nil {
return err
}
rs, ok := ts.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}
accessToken := rs.Primary.Attributes["access_token"]
if accessToken == "" {
return fmt.Errorf("access token is empty")
}
projectIDString := rs.Primary.Attributes["project_id"]
projectID, err := strconv.Atoi(projectIDString)
if err != nil {
Expand Down Expand Up @@ -506,7 +506,7 @@ func (s *AccSuite) checkProjectAccessToken(resourceName string) resource.TestChe
// project access token is present in the list of all project access tokens.
func (s *AccSuite) checkProjectAccessTokenInTokenList(rn string) resource.TestCheckFunc {
return func(ts *terraform.State) error {
accessToken, err := s.getResourceIDString(ts, rn)
accessToken, err := s.getResourceAttrString(ts, rn, "access_token")
s.Nil(err)
projectID, err := s.getResourceAttrInt(ts, rn, "project_id")
s.Nil(err)
Expand All @@ -531,7 +531,7 @@ func importIdProjectAccessToken(resourceName string) resource.ImportStateIdFunc
return "", fmt.Errorf("not found: %s", resourceName)
}
projectID := rs.Primary.Attributes["project_id"]
accessToken := rs.Primary.ID
accessToken := rs.Primary.Attributes["access_token"]

return fmt.Sprintf("%s/%s", projectID, accessToken), nil
}
Expand Down

0 comments on commit 32838f2

Please sign in to comment.