From dea81198d97142af1d8a0e660c62d8424fd83cf1 Mon Sep 17 00:00:00 2001 From: Chris Zembower Date: Tue, 9 Jan 2024 13:27:06 -0500 Subject: [PATCH] initial commit: handle missing resources --- vault/resource_github_auth_backend.go | 14 +++++++------- vault/resource_github_team.go | 13 +++++++++++-- vault/resource_github_user.go | 13 +++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/vault/resource_github_auth_backend.go b/vault/resource_github_auth_backend.go index d055f93cc..b5cefb116 100644 --- a/vault/resource_github_auth_backend.go +++ b/vault/resource_github_auth_backend.go @@ -179,13 +179,6 @@ func githubAuthBackendRead(d *schema.ResourceData, meta interface{}) error { path := "auth/" + d.Id() configPath := path + "/config" - log.Printf("[DEBUG] Reading github auth mount from '%q'", path) - mount, err := authMountInfoGet(client, d.Id()) - if err != nil { - return fmt.Errorf("error reading github auth mount from '%q': %w", path, err) - } - log.Printf("[INFO] Read github auth mount from '%q'", path) - log.Printf("[DEBUG] Reading github auth config from '%q'", configPath) resp, err := client.Logical().Read(configPath) if err != nil { @@ -199,6 +192,13 @@ func githubAuthBackendRead(d *schema.ResourceData, meta interface{}) error { return nil } + log.Printf("[DEBUG] Reading github auth mount from '%q'", path) + mount, err := authMountInfoGet(client, d.Id()) + if err != nil { + return fmt.Errorf("error reading github auth mount from '%q': %w", path, err) + } + log.Printf("[INFO] Read github auth mount from '%q'", path) + log.Printf("[DEBUG] Reading github auth tune from '%q/tune'", path) rawTune, err := authMountTuneGet(client, path) if err != nil { diff --git a/vault/resource_github_team.go b/vault/resource_github_team.go index cfd171b1b..2ba090e69 100644 --- a/vault/resource_github_team.go +++ b/vault/resource_github_team.go @@ -4,7 +4,6 @@ package vault import ( - "fmt" "log" "strings" @@ -101,10 +100,20 @@ func githubTeamRead(d *schema.ResourceData, meta interface{}) error { return err } + // If the auth method is not enabled, dt is nil + if dt == nil { + log.Printf("[WARN] Github team mapping from '%q' is null, removing from state", d.Id()) + d.SetId("") + return nil + } + if v, ok := dt.Data["key"]; ok { d.Set("team", v.(string)) } else { - return fmt.Errorf("github team information not found at path: '%v'", d.Id()) + // If the method is enabled but the team is not mapped, the API responds 200 with an empty Data object + log.Printf("[WARN] Github team information from '%q' not found, removing from state", d.Id()) + d.SetId("") + return nil } if v, ok := dt.Data["value"]; ok { diff --git a/vault/resource_github_user.go b/vault/resource_github_user.go index 1a9ea2832..de33d1f4e 100644 --- a/vault/resource_github_user.go +++ b/vault/resource_github_user.go @@ -4,7 +4,6 @@ package vault import ( - "fmt" "log" "strings" @@ -100,10 +99,20 @@ func githubUserRead(d *schema.ResourceData, meta interface{}) error { return err } + // If the auth method is not enabled, dt is nil + if dt == nil { + log.Printf("[WARN] Github user mapping from '%q' is null, removing from state", d.Id()) + d.SetId("") + return nil + } + if v, ok := dt.Data["key"]; ok { d.Set("user", v.(string)) } else { - return fmt.Errorf("github user information not found at path: '%v'", d.Id()) + // If the method is enabled but the user is not mapped, the API responds 200 with an empty Data object + log.Printf("[WARN] Github user information from '%q' not found, removing from state", d.Id()) + d.SetId("") + return nil } if v, ok := dt.Data["value"]; ok {