Skip to content

Commit

Permalink
Avoid crash when importing invalid ID
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Aug 15, 2017
1 parent 4722806 commit d821aa6
Show file tree
Hide file tree
Showing 21 changed files with 327 additions and 81 deletions.
25 changes: 19 additions & 6 deletions kubernetes/resource_kubernetes_config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func resourceKubernetesConfigMapCreate(d *schema.ResourceData, meta interface{})
func resourceKubernetesConfigMapRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Reading config map %s", name)
cfgMap, err := conn.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
if err != nil {
Expand All @@ -76,7 +79,10 @@ func resourceKubernetesConfigMapRead(d *schema.ResourceData, meta interface{}) e
func resourceKubernetesConfigMapUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

ops := patchMetadata("metadata.0.", "/metadata/", d)
if d.HasChange("data") {
Expand All @@ -102,9 +108,12 @@ func resourceKubernetesConfigMapUpdate(d *schema.ResourceData, meta interface{})
func resourceKubernetesConfigMapDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Deleting config map: %#v", name)
err := conn.CoreV1().ConfigMaps(namespace).Delete(name, &metav1.DeleteOptions{})
err = conn.CoreV1().ConfigMaps(namespace).Delete(name, &metav1.DeleteOptions{})
if err != nil {
return err
}
Expand All @@ -118,9 +127,13 @@ func resourceKubernetesConfigMapDelete(d *schema.ResourceData, meta interface{})
func resourceKubernetesConfigMapExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}

log.Printf("[INFO] Checking config map %s", name)
_, err := conn.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
_, err = conn.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
Expand Down
10 changes: 8 additions & 2 deletions kubernetes/resource_kubernetes_config_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ func testAccCheckKubernetesConfigMapDestroy(s *terraform.State) error {
if rs.Type != "kubernetes_config_map" {
continue
}
namespace, name := idParts(rs.Primary.ID)
namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}
resp, err := conn.CoreV1().ConfigMaps(namespace).Get(name, meta_v1.GetOptions{})
if err == nil {
if resp.Name == rs.Primary.ID {
Expand All @@ -207,7 +210,10 @@ func testAccCheckKubernetesConfigMapExists(n string, obj *api.ConfigMap) resourc
}

conn := testAccProvider.Meta().(*kubernetes.Clientset)
namespace, name := idParts(rs.Primary.ID)
namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}
out, err := conn.CoreV1().ConfigMaps(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return err
Expand Down
25 changes: 19 additions & 6 deletions kubernetes/resource_kubernetes_horizontal_pod_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ func resourceKubernetesHorizontalPodAutoscalerCreate(d *schema.ResourceData, met
func resourceKubernetesHorizontalPodAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Reading horizontal pod autoscaler %s", name)
svc, err := conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
Expand All @@ -130,7 +133,10 @@ func resourceKubernetesHorizontalPodAutoscalerRead(d *schema.ResourceData, meta
func resourceKubernetesHorizontalPodAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

ops := patchMetadata("metadata.0.", "/metadata/", d)
if d.HasChange("spec") {
Expand All @@ -155,9 +161,12 @@ func resourceKubernetesHorizontalPodAutoscalerUpdate(d *schema.ResourceData, met
func resourceKubernetesHorizontalPodAutoscalerDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Deleting horizontal pod autoscaler: %#v", name)
err := conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Delete(name, &meta_v1.DeleteOptions{})
err = conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Delete(name, &meta_v1.DeleteOptions{})
if err != nil {
return err
}
Expand All @@ -171,9 +180,13 @@ func resourceKubernetesHorizontalPodAutoscalerDelete(d *schema.ResourceData, met
func resourceKubernetesHorizontalPodAutoscalerExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}

log.Printf("[INFO] Checking horizontal pod autoscaler %s", name)
_, err := conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(name, meta_v1.GetOptions{})
_, err = conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
Expand Down
14 changes: 12 additions & 2 deletions kubernetes/resource_kubernetes_horizontal_pod_autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ func testAccCheckKubernetesHorizontalPodAutoscalerDestroy(s *terraform.State) er
if rs.Type != "kubernetes_horizontal_pod_autoscaler" {
continue
}
namespace, name := idParts(rs.Primary.ID)

namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}

resp, err := conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(name, meta_v1.GetOptions{})
if err == nil {
if resp.Namespace == namespace && resp.Name == name {
Expand All @@ -181,7 +186,12 @@ func testAccCheckKubernetesHorizontalPodAutoscalerExists(n string, obj *api.Hori
}

conn := testAccProvider.Meta().(*kubernetes.Clientset)
namespace, name := idParts(rs.Primary.ID)

namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}

out, err := conn.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return err
Expand Down
26 changes: 20 additions & 6 deletions kubernetes/resource_kubernetes_limit_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func resourceKubernetesLimitRangeCreate(d *schema.ResourceData, meta interface{}
func resourceKubernetesLimitRangeRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Reading limit range %s", name)
limitRange, err := conn.CoreV1().LimitRanges(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
Expand All @@ -129,7 +132,10 @@ func resourceKubernetesLimitRangeRead(d *schema.ResourceData, meta interface{})
func resourceKubernetesLimitRangeUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

ops := patchMetadata("metadata.0.", "/metadata/", d)
if d.HasChange("spec") {
Expand Down Expand Up @@ -160,9 +166,13 @@ func resourceKubernetesLimitRangeUpdate(d *schema.ResourceData, meta interface{}
func resourceKubernetesLimitRangeDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Deleting limit range: %#v", name)
err := conn.CoreV1().LimitRanges(namespace).Delete(name, &meta_v1.DeleteOptions{})
err = conn.CoreV1().LimitRanges(namespace).Delete(name, &meta_v1.DeleteOptions{})
if err != nil {
return err
}
Expand All @@ -176,9 +186,13 @@ func resourceKubernetesLimitRangeDelete(d *schema.ResourceData, meta interface{}
func resourceKubernetesLimitRangeExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}

log.Printf("[INFO] Checking limit range %s", name)
_, err := conn.CoreV1().LimitRanges(namespace).Get(name, meta_v1.GetOptions{})
_, err = conn.CoreV1().LimitRanges(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
Expand Down
14 changes: 12 additions & 2 deletions kubernetes/resource_kubernetes_limit_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@ func testAccCheckKubernetesLimitRangeDestroy(s *terraform.State) error {
if rs.Type != "kubernetes_limit_range" {
continue
}
namespace, name := idParts(rs.Primary.ID)

namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}

resp, err := conn.CoreV1().LimitRanges(namespace).Get(name, meta_v1.GetOptions{})
if err == nil {
if resp.Namespace == namespace && resp.Name == name {
Expand All @@ -283,7 +288,12 @@ func testAccCheckKubernetesLimitRangeExists(n string, obj *api.LimitRange) resou
}

conn := testAccProvider.Meta().(*kubernetes.Clientset)
namespace, name := idParts(rs.Primary.ID)

namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}

out, err := conn.CoreV1().LimitRanges(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return err
Expand Down
28 changes: 22 additions & 6 deletions kubernetes/resource_kubernetes_persistent_volume_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ func resourceKubernetesPersistentVolumeClaimCreate(d *schema.ResourceData, meta
func resourceKubernetesPersistentVolumeClaimRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Reading persistent volume claim %s", name)
claim, err := conn.CoreV1().PersistentVolumeClaims(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
Expand All @@ -227,7 +231,11 @@ func resourceKubernetesPersistentVolumeClaimRead(d *schema.ResourceData, meta in

func resourceKubernetesPersistentVolumeClaimUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
namespace, name := idParts(d.Id())

namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

ops := patchMetadata("metadata.0.", "/metadata/", d)
// The whole spec is ForceNew = nothing to update there
Expand All @@ -249,9 +257,13 @@ func resourceKubernetesPersistentVolumeClaimUpdate(d *schema.ResourceData, meta
func resourceKubernetesPersistentVolumeClaimDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Deleting persistent volume claim: %#v", name)
err := conn.CoreV1().PersistentVolumeClaims(namespace).Delete(name, &meta_v1.DeleteOptions{})
err = conn.CoreV1().PersistentVolumeClaims(namespace).Delete(name, &meta_v1.DeleteOptions{})
if err != nil {
return err
}
Expand All @@ -265,9 +277,13 @@ func resourceKubernetesPersistentVolumeClaimDelete(d *schema.ResourceData, meta
func resourceKubernetesPersistentVolumeClaimExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}

log.Printf("[INFO] Checking persistent volume claim %s", name)
_, err := conn.CoreV1().PersistentVolumeClaims(namespace).Get(name, meta_v1.GetOptions{})
_, err = conn.CoreV1().PersistentVolumeClaims(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
Expand Down
14 changes: 12 additions & 2 deletions kubernetes/resource_kubernetes_persistent_volume_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,12 @@ func testAccCheckKubernetesPersistentVolumeClaimDestroy(s *terraform.State) erro
if rs.Type != "kubernetes_persistent_volume_claim" {
continue
}
namespace, name := idParts(rs.Primary.ID)

namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}

resp, err := conn.CoreV1().PersistentVolumeClaims(namespace).Get(name, meta_v1.GetOptions{})
if err == nil {
if resp.Namespace == namespace && resp.Name == name {
Expand All @@ -426,7 +431,12 @@ func testAccCheckKubernetesPersistentVolumeClaimExists(n string, obj *api.Persis
}

conn := testAccProvider.Meta().(*kubernetes.Clientset)
namespace, name := idParts(rs.Primary.ID)

namespace, name, err := idParts(rs.Primary.ID)
if err != nil {
return err
}

out, err := conn.CoreV1().PersistentVolumeClaims(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return err
Expand Down
30 changes: 24 additions & 6 deletions kubernetes/resource_kubernetes_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ func resourceKubernetesPodCreate(d *schema.ResourceData, meta interface{}) error

func resourceKubernetesPodUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
namespace, name := idParts(d.Id())

namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

ops := patchMetadata("metadata.0.", "/metadata/", d)
if d.HasChange("spec") {
specOps, err := patchPodSpec("/spec", "spec.0.", d)
Expand Down Expand Up @@ -123,7 +128,11 @@ func resourceKubernetesPodUpdate(d *schema.ResourceData, meta interface{}) error

func resourceKubernetesPodRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
namespace, name := idParts(d.Id())

namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Reading pod %s", name)
pod, err := conn.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
Expand Down Expand Up @@ -153,9 +162,14 @@ func resourceKubernetesPodRead(d *schema.ResourceData, meta interface{}) error {

func resourceKubernetesPodDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
namespace, name := idParts(d.Id())

namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Deleting pod: %#v", name)
err := conn.CoreV1().Pods(namespace).Delete(name, nil)
err = conn.CoreV1().Pods(namespace).Delete(name, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -186,9 +200,13 @@ func resourceKubernetesPodDelete(d *schema.ResourceData, meta interface{}) error
func resourceKubernetesPodExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}

log.Printf("[INFO] Checking pod %s", name)
_, err := conn.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
_, err = conn.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
Expand Down
Loading

0 comments on commit d821aa6

Please sign in to comment.