From 32838f24da5ae8b7ee1a1a06d569774b6025228e Mon Sep 17 00:00:00 2001 From: Pawel Date: Tue, 3 Sep 2024 00:42:14 -0700 Subject: [PATCH] added a hasher to modify access token --- rollbar/resource_project_access_token.go | 13 ++++++++++--- rollbar/test1/resource_project_access_token_test.go | 12 ++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/rollbar/resource_project_access_token.go b/rollbar/resource_project_access_token.go index a1f5407a..f18395e4 100644 --- a/rollbar/resource_project_access_token.go +++ b/rollbar/resource_project_access_token.go @@ -24,10 +24,11 @@ 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" @@ -35,6 +36,12 @@ import ( "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, @@ -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) } @@ -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 } diff --git a/rollbar/test1/resource_project_access_token_test.go b/rollbar/test1/resource_project_access_token_test.go index 9c2dbc30..3edabf71 100644 --- a/rollbar/test1/resource_project_access_token_test.go +++ b/rollbar/test1/resource_project_access_token_test.go @@ -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 { @@ -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) @@ -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 }