Skip to content

Commit

Permalink
Merge pull request #9 from komodorio/fix-check-error-404
Browse files Browse the repository at this point in the history
fix(provider): Handle resource not found
  • Loading branch information
Moran-k committed Jan 21, 2024
2 parents d1660a0 + 0912ad2 commit 03ea189
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=github.com
NAMESPACE=komodorio
NAME=komodor
BINARY=terraform-provider-${NAME}
VERSION=1.0.6
VERSION=1.0.7
OS_ARCH=darwin_amd64

default: install
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Clone the repository:

```sh
mkdir -p $GOPATH/src/github.com/terraform-providers; cd "$_"
git clone https://github.com/komodor/terraform-provider-komodor.git
git clone https://github.com/komodorio/terraform-provider-komodor.git
```

Change to the clone directory and run make to install the dependent tooling needed to test and build the provider.
Expand Down
2 changes: 1 addition & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
komodor = {
version = "1.0.4"
version = ">= 1.0.6"
source = "komodorio/komodor"
}
}
Expand Down
12 changes: 6 additions & 6 deletions komodor/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ func NewClient(apiKey string) *Client {
}
}

func (c *Client) executeHttpRequest(method string, url string, body *[]byte) ([]byte, error) {
func (c *Client) executeHttpRequest(method string, url string, body *[]byte) ([]byte, int, error) {
var reader io.Reader
if body != nil {
reader = bytes.NewReader(*body)
}
req, err := http.NewRequest(method, url, reader)
if err != nil {
return nil, err
return nil, 0, err
}
req.Header.Set("x-api-key", c.ApiKey)
req.Header.Set("Content-Type", "application/json")
res, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
return nil, res.StatusCode, err
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
return nil, res.StatusCode, err
}
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated || res.StatusCode == http.StatusNoContent {
return resBody, nil
return resBody, res.StatusCode, nil
} else {
return nil, fmt.Errorf("%d %s", res.StatusCode, resBody)
return resBody, res.StatusCode, fmt.Errorf("%d %s", res.StatusCode, resBody)
}
}
18 changes: 9 additions & 9 deletions komodor/monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Monitor struct {
}

func (c *Client) GetMonitors() ([]Monitor, error) {
res, err := c.executeHttpRequest(http.MethodGet, MonitorsUrl, nil)
res, _, err := c.executeHttpRequest(http.MethodGet, MonitorsUrl, nil)
if err != nil {
return nil, err
}
Expand All @@ -95,26 +95,26 @@ func (c *Client) GetMonitors() ([]Monitor, error) {
return monitors, nil
}

func (c *Client) GetMonitor(id string) (*Monitor, error) {
res, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(MonitorsUrl+"/%s", id), nil)
func (c *Client) GetMonitor(id string) (*Monitor, int, error) {
res, statusCode, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(MonitorsUrl+"/%s", id), nil)
if err != nil {
return nil, err
return nil, statusCode, err
}
var monitor Monitor
err = json.Unmarshal(res, &monitor)
if err != nil {
return nil, err
return nil, statusCode, err
}

return &monitor, nil
return &monitor, statusCode, nil
}

func (c *Client) UpdateMonitor(id string, m *NewMonitor) ([]byte, error) {
jsonMonitor, err := json.Marshal(m)
if err != nil {
return nil, err
}
res, err := c.executeHttpRequest(http.MethodPut, fmt.Sprintf(MonitorsUrl+"/%s", id), &jsonMonitor)
res, _, err := c.executeHttpRequest(http.MethodPut, fmt.Sprintf(MonitorsUrl+"/%s", id), &jsonMonitor)
if err != nil {
return nil, err
}
Expand All @@ -127,7 +127,7 @@ func (c *Client) CreateMonitor(m *NewMonitor) (*Monitor, error) {
if err != nil {
return nil, err
}
res, err := c.executeHttpRequest(http.MethodPost, MonitorsUrl, &jsonMonitor)
res, _, err := c.executeHttpRequest(http.MethodPost, MonitorsUrl, &jsonMonitor)

if err != nil {
return nil, err
Expand All @@ -143,7 +143,7 @@ func (c *Client) CreateMonitor(m *NewMonitor) (*Monitor, error) {

func (c *Client) DeleteMonitor(id string) error {
requestUrl := strings.Join([]string{MonitorsUrl, "/", id}, "")
_, err := c.executeHttpRequest(http.MethodDelete, requestUrl, nil)
_, _, err := c.executeHttpRequest(http.MethodDelete, requestUrl, nil)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions komodor/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type NewPolicy struct {
}

func (c *Client) GetPolicies() ([]Policy, error) {
res, err := c.executeHttpRequest(http.MethodGet, PoliciesUrl, nil)
res, _, err := c.executeHttpRequest(http.MethodGet, PoliciesUrl, nil)

if err != nil {
return nil, err
Expand All @@ -48,21 +48,21 @@ func (c *Client) GetPolicies() ([]Policy, error) {
return policies, nil
}

func (c *Client) GetPolicy(id string) (*Policy, error) {
func (c *Client) GetPolicy(id string) (*Policy, int, error) {
var policy Policy

res, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(PoliciesUrl+"/%s", id), nil)
res, statusCode, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(PoliciesUrl+"/%s", id), nil)

if err != nil {
return nil, err
return nil, statusCode, err
}

err = json.Unmarshal(res, &policy)
if err != nil {
return nil, err
return nil, statusCode, err
}

return &policy, nil
return &policy, statusCode, nil
}

func (c *Client) GetPolicyByName(name string) (*Policy, error) {
Expand All @@ -87,7 +87,7 @@ func (c *Client) CreatePolicy(p *NewPolicy) (*Policy, error) {
if err != nil {
return nil, err
}
res, err := c.executeHttpRequest(http.MethodPost, PoliciesUrl, &jsonPolicy)
res, _, err := c.executeHttpRequest(http.MethodPost, PoliciesUrl, &jsonPolicy)

if err != nil {
return nil, err
Expand All @@ -107,7 +107,7 @@ func (c *Client) DeletePolicy(id string) error {
if err != nil {
return err
}
_, err = c.executeHttpRequest(http.MethodDelete, PoliciesUrl, &requestBody)
_, _, err = c.executeHttpRequest(http.MethodDelete, PoliciesUrl, &requestBody)
if err != nil {
return err
}
Expand All @@ -119,7 +119,7 @@ func (c *Client) UpdatePolicy(id string, p *NewPolicy) (*Policy, error) {
if err != nil {
return nil, err
}
res, err := c.executeHttpRequest(http.MethodPut, fmt.Sprintf(PoliciesUrl+"/%s", id), &jsonPolicy)
res, _, err := c.executeHttpRequest(http.MethodPut, fmt.Sprintf(PoliciesUrl+"/%s", id), &jsonPolicy)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions komodor/resource_komodor_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ func resourceKomodorMonitorRead(ctx context.Context, d *schema.ResourceData, met
client := meta.(*Client)
id := d.Id()

monitor, err := client.GetMonitor(id)
monitor, statusCode, err := client.GetMonitor(id)
if err != nil {
if err.Error() == "404" {
if statusCode == 404 {
log.Printf("[DEBUG] Monitor (%s) was not found - removing from state", d.Id())
d.SetId("")
return nil
Expand Down
4 changes: 2 additions & 2 deletions komodor/resource_komodor_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func resourceKomodorPolicyRead(ctx context.Context, d *schema.ResourceData, meta
client := meta.(*Client)
id := d.Id()

policy, err := client.GetPolicy(id)
policy, statusCode, err := client.GetPolicy(id)
if err != nil {
if err.Error() == "404" {
if statusCode == 404 {
log.Printf("[DEBUG] Policy (%s) was not found - removing from state", d.Id())
d.SetId("")
return nil
Expand Down
4 changes: 2 additions & 2 deletions komodor/resource_komodor_policy_role_attachement.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func resourcePolicyRoleAttachmentRead(ctx context.Context, d *schema.ResourceDat
client := meta.(*Client)
roleId := d.Get("role").(string)

rolePolicyObject, err := client.GetRolePoliciesObject(roleId)
rolePolicyObject, statusCode, err := client.GetRolePoliciesObject(roleId)
if err != nil {
if err.Error() == "404" {
if statusCode == 404 {
log.Printf("[DEBUG] Role-Policy object (%s) was not found - removing from state", d.Id())
d.SetId("")
return nil
Expand Down
4 changes: 2 additions & 2 deletions komodor/resource_komodor_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func resourceKomodorRoleRead(ctx context.Context, d *schema.ResourceData, meta i
client := meta.(*Client)
id := d.Id()

role, err := client.GetRole(id)
role, statusCode, err := client.GetRole(id)
if err != nil {
if err.Error() == "404" {
if statusCode == 404 {
log.Printf("[DEBUG] Role (%s) was not found - removing from state", d.Id())
d.SetId("")
return nil
Expand Down
14 changes: 7 additions & 7 deletions komodor/role_policy_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ func (c *Client) AttachPolicy(policyId string, roleId string) error {
if err != nil {
return err
}
_, err = c.executeHttpRequest(http.MethodPost, PolicyRoleAttachmentUrl, &requestBody)
_, _, err = c.executeHttpRequest(http.MethodPost, PolicyRoleAttachmentUrl, &requestBody)
if err != nil {
return err
}

return nil
}

func (c *Client) GetRolePoliciesObject(roleId string) ([]RolePolicy, error) {
func (c *Client) GetRolePoliciesObject(roleId string) ([]RolePolicy, int, error) {
var rolePolicies []RolePolicy

res, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(DefaultEndpoint+"/rbac/roles/%s/policies", roleId), nil)
res, statusCode, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(DefaultEndpoint+"/rbac/roles/%s/policies", roleId), nil)
if err != nil {
return nil, err
return nil, statusCode, err
}

err = json.Unmarshal([]byte(res), &rolePolicies)
if err != nil {
return nil, err
return nil, statusCode, err
}

return rolePolicies, nil
return rolePolicies, statusCode, nil
}

func (c *Client) DetachPolicy(policyId string, roleId string) error {
Expand All @@ -49,7 +49,7 @@ func (c *Client) DetachPolicy(policyId string, roleId string) error {
if err != nil {
return err
}
_, err = c.executeHttpRequest(http.MethodDelete, PolicyRoleAttachmentUrl, &requestBody)
_, _, err = c.executeHttpRequest(http.MethodDelete, PolicyRoleAttachmentUrl, &requestBody)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions komodor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type NewRole struct {
}

func (c *Client) GetRoles() ([]Role, error) {
res, err := c.executeHttpRequest(http.MethodGet, RolesUrl, nil)
res, _, err := c.executeHttpRequest(http.MethodGet, RolesUrl, nil)

if err != nil {
return nil, err
Expand Down Expand Up @@ -53,21 +53,21 @@ func (c *Client) GetRoleByName(name string) (*Role, error) {
return targetRole, nil
}

func (c *Client) GetRole(id string) (*Role, error) {
func (c *Client) GetRole(id string) (*Role, int, error) {
var role Role

res, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(RolesUrl+"/%s", id), nil)
res, statusCode, err := c.executeHttpRequest(http.MethodGet, fmt.Sprintf(RolesUrl+"/%s", id), nil)

if err != nil {
return nil, err
return nil, statusCode, err
}

err = json.Unmarshal(res, &role)
if err != nil {
return nil, err
return nil, statusCode, err
}

return &role, nil
return &role, statusCode, nil
}

func (c *Client) CreateRole(role *NewRole) (*Role, error) {
Expand All @@ -76,7 +76,7 @@ func (c *Client) CreateRole(role *NewRole) (*Role, error) {
if err != nil {
return nil, err
}
res, err := c.executeHttpRequest(http.MethodPost, RolesUrl, &requestBody)
res, _, err := c.executeHttpRequest(http.MethodPost, RolesUrl, &requestBody)

if err != nil {
return nil, err
Expand All @@ -97,7 +97,7 @@ func (c *Client) DeleteRole(id string) error {
return err
}

_, err = c.executeHttpRequest(http.MethodDelete, RolesUrl, &requestBody)
_, _, err = c.executeHttpRequest(http.MethodDelete, RolesUrl, &requestBody)
if err != nil {
return err
}
Expand Down

0 comments on commit 03ea189

Please sign in to comment.