Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Check for namespace existence for resources that are in namespaces (#48)
Browse files Browse the repository at this point in the history
* handle namespace being deleted

* set exists for ns prefs
  • Loading branch information
umairidris authored Feb 26, 2020
1 parent 3b5d037 commit abb7ac3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
11 changes: 10 additions & 1 deletion cdap/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cdap
import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -153,7 +154,15 @@ func resourceApplicationDelete(d *schema.ResourceData, m interface{}) error {
func resourceApplicationExists(d *schema.ResourceData, m interface{}) (bool, error) {
config := m.(*Config)
name := d.Get("name").(string)
addr := urlJoin(config.host, "/v3/namespaces", d.Get("namespace").(string), "/apps")

namespace := d.Get("namespace").(string)
if exists, err := namespaceExists(config, namespace); err != nil {
return false, fmt.Errorf("failed to check for existence of namespace %q: %v", namespace, err)
} else if !exists {
return false, nil
}

addr := urlJoin(config.host, "/v3/namespaces", namespace, "/apps")
req, err := http.NewRequest(http.MethodGet, addr, nil)
if err != nil {
return false, err
Expand Down
11 changes: 10 additions & 1 deletion cdap/resource_local_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cdap
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -191,7 +192,15 @@ func resourceLocalArtifactDelete(d *schema.ResourceData, m interface{}) error {
func resourceLocalArtifactExists(d *schema.ResourceData, m interface{}) (bool, error) {
config := m.(*Config)
name := d.Get("name").(string)
addr := urlJoin(config.host, "/v3/namespaces", d.Get("namespace").(string), "/artifacts")

namespace := d.Get("namespace").(string)
if exists, err := namespaceExists(config, namespace); err != nil {
return false, fmt.Errorf("failed to check for existence of namespace %q: %v", namespace, err)
} else if !exists {
return false, nil
}

addr := urlJoin(config.host, "/v3/namespaces", namespace, "/artifacts")

req, err := http.NewRequest(http.MethodGet, addr, nil)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions cdap/resource_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ func resourceNamespaceDelete(d *schema.ResourceData, m interface{}) error {
func resourceNamespaceExists(d *schema.ResourceData, m interface{}) (bool, error) {
config := m.(*Config)
name := d.Get("name").(string)
return namespaceExists(config, name)
}

func namespaceExists(config *Config, name string) (bool, error) {
// Default namespace should always exist.
if name == defaultNamespace {
return true, nil
}

addr := urlJoin(config.host, "/v3/namespaces")

req, err := http.NewRequest(http.MethodGet, addr, nil)
Expand Down
7 changes: 7 additions & 0 deletions cdap/resource_namespace_preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func resourceNamespacePreferences() *schema.Resource {
Create: resourceNamespacePreferencesCreate,
Read: resourceNamespacePreferencesRead,
Delete: resourceNamespacePreferencesDelete,
Exists: resourceNamespacePreferencesExist,

Schema: map[string]*schema.Schema{
"namespace": {
Expand Down Expand Up @@ -90,3 +91,9 @@ func resourceNamespacePreferencesDelete(d *schema.ResourceData, m interface{}) e
_, err = httpCall(config.httpClient, req)
return err
}

func resourceNamespacePreferencesExist(d *schema.ResourceData, m interface{}) (bool, error) {
config := m.(*Config)
namespace := d.Get("namespace").(string)
return namespaceExists(config, namespace)
}
11 changes: 10 additions & 1 deletion cdap/resource_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cdap
import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -198,7 +199,15 @@ func resourceProfileDelete(d *schema.ResourceData, m interface{}) error {
func resourceProfileExists(d *schema.ResourceData, m interface{}) (bool, error) {
config := m.(*Config)
name := d.Get("name").(string)
addr := urlJoin(config.host, "/v3/namespaces", d.Get("namespace").(string), "/profiles")

namespace := d.Get("namespace").(string)
if exists, err := namespaceExists(config, namespace); err != nil {
return false, fmt.Errorf("failed to check for existence of namespace %q: %v", namespace, err)
} else if !exists {
return false, nil
}

addr := urlJoin(config.host, "/v3/namespaces", namespace, "/profiles")

req, err := http.NewRequest(http.MethodGet, addr, nil)
if err != nil {
Expand Down

0 comments on commit abb7ac3

Please sign in to comment.