From 307d3bca8f88459a6ec3054141dc86cc728556b1 Mon Sep 17 00:00:00 2001 From: Pawel Date: Mon, 2 Sep 2024 23:39:35 -0700 Subject: [PATCH 1/2] fix id for project access token resource --- rollbar/resource_project_access_token.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/rollbar/resource_project_access_token.go b/rollbar/resource_project_access_token.go index d7750e34..a1f5407a 100644 --- a/rollbar/resource_project_access_token.go +++ b/rollbar/resource_project_access_token.go @@ -27,6 +27,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -156,15 +157,15 @@ func resourceProjectAccessTokenCreate(ctx context.Context, d *schema.ResourceDat return diag.FromErr(err) } - d.SetId(pat.AccessToken) - + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) + mustSet(d, "access_token", pat.AccessToken) return resourceProjectAccessTokenRead(ctx, d, m) } func resourceProjectAccessTokenRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { var diags diag.Diagnostics - accessToken := d.Id() + accessToken := d.Get("access_token").(string) projectID := d.Get("project_id").(int) l := log.With(). Str("accessToken", accessToken). @@ -195,7 +196,7 @@ func resourceProjectAccessTokenRead(ctx context.Context, d *schema.ResourceData, } func resourceProjectAccessTokenUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - accessToken := d.Id() + accessToken := d.Get("access_token").(string) projectID := d.Get("project_id").(int) size := d.Get("rate_limit_window_size").(int) count := d.Get("rate_limit_window_count").(int) @@ -220,7 +221,7 @@ func resourceProjectAccessTokenUpdate(ctx context.Context, d *schema.ResourceDat } func resourceProjectAccessTokenDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - accessToken := d.Id() + accessToken := d.Get("access_token").(string) projectID := d.Get("project_id").(int) l := log.With(). @@ -258,6 +259,7 @@ func resourceProjectAccessTokenImporter(_ context.Context, d *schema.ResourceDat Str("access_token", accessToken). Send() mustSet(d, "project_id", projectID) - d.SetId(accessToken) + mustSet(d, "access_token", accessToken) + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) return []*schema.ResourceData{d}, nil } From 32838f24da5ae8b7ee1a1a06d569774b6025228e Mon Sep 17 00:00:00 2001 From: Pawel Date: Tue, 3 Sep 2024 00:42:14 -0700 Subject: [PATCH 2/2] 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 }