Skip to content

Commit

Permalink
fix: Refresh and destroy on DeploymentSettings fails with 404 if ther…
Browse files Browse the repository at this point in the history
…e is an out of band deletion #146
  • Loading branch information
glena committed Aug 1, 2023
1 parent dd5e1dd commit c3d27ac
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
9 changes: 5 additions & 4 deletions provider/pkg/internal/pulumiapi/deployment_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pulumiapi

import (
"context"
"errors"
"fmt"
"net/http"
"path"
Expand Down Expand Up @@ -81,9 +80,11 @@ func (c *Client) GetDeploymentSettings(ctx context.Context, stack StackName) (*D
var ds DeploymentSettings
_, err := c.do(ctx, http.MethodGet, apiPath, nil, &ds)
if err != nil {
var errResp *errorResponse
if errors.As(err, &errResp) && errResp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("deployment settings for stack (%s) not found", stack.String())
statusCode := GetErrorStatusCode(err)
if statusCode == http.StatusNotFound {
// Important: do now wrap this error so the provider knows to handle it as a
// deleted resource
return nil, err
}
return nil, fmt.Errorf("failed to get deployment settings for stack (%s): %w", stack.String(), err)
}
Expand Down
15 changes: 13 additions & 2 deletions provider/pkg/internal/pulumiapi/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -13,7 +13,10 @@
// limitations under the License.
package pulumiapi

import "fmt"
import (
"errors"
"fmt"
)

// errorResponse is returned from pulumi service api when there's been an error
type errorResponse struct {
Expand All @@ -24,3 +27,11 @@ type errorResponse struct {
func (err *errorResponse) Error() string {
return fmt.Sprintf("%d API error: %s", err.StatusCode, err.Message)
}

func GetErrorStatusCode(err error) int {
var errResp *errorResponse
if errors.As(err, &errResp) {
return errResp.StatusCode
}
return 0
}
4 changes: 2 additions & 2 deletions provider/pkg/internal/pulumiapi/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ func (c *Client) updateTeamMembership(ctx context.Context, orgName, teamName, us
func (c *Client) AddMemberToTeam(ctx context.Context, orgName, teamName, userName string) error {
err := c.updateTeamMembership(ctx, orgName, teamName, userName, "add")
if err != nil {
var errResp *errorResponse
if errors.As(err, &errResp) && errResp.StatusCode == http.StatusConflict {
statusCode := GetErrorStatusCode(err)
if statusCode == http.StatusConflict {
// ignore 409 since that means the team member is already added
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions provider/pkg/provider/deployment_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"net/http"
"path"

pbempty "github.com/golang/protobuf/ptypes/empty"
Expand Down Expand Up @@ -529,7 +530,15 @@ func (ds *PulumiServiceDeploymentSettingsResource) Read(req *pulumirpc.ReadReque
}
settings, err := ds.client.GetDeploymentSettings(ctx, stack)
if err != nil {
statusCode := pulumiapi.GetErrorStatusCode(err)
if statusCode == http.StatusNotFound {
// deleteResponse causes the resource to be deleted from the state.
var deleteResponse = &pulumirpc.ReadResponse{Id: "", Properties: nil}
// If it's a 404 error, this resource was probably deleted.
return deleteResponse, nil
}
return nil, err

}

dsInput := PulumiServiceDeploymentSettingsInput{
Expand Down

0 comments on commit c3d27ac

Please sign in to comment.