From 0fb3c929467fa5ce2da6005fd0b9c0ed56b382ea Mon Sep 17 00:00:00 2001 From: Samuel Gordalina Date: Tue, 18 Sep 2018 12:10:42 -0700 Subject: [PATCH] Add resource_count fixes #4 --- README.md | 3 ++- main.go | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8f6e65ff..0ff76594 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ provider "k8s" { soon as call is completed. This may impact performance if the code runs on a shared system because and the global tempdir is used. -The k8s Terraform provider introduces a single Terraform resource, a `k8s_manifest`. The resource contains a `content` field, which contains a raw manifest. +The k8s Terraform provider introduces a single Terraform resource, a `k8s_manifest`. The resource contains a `content` field, which contains a raw manifest, and an optional `resource_count` field which when present asserts that the number of resources created are equal to `resource_count`. ```hcl variable "replicas" { @@ -58,6 +58,7 @@ data "template_file" "nginx-deployment" { resource "k8s_manifest" "nginx-deployment" { content = "${data.template_file.nginx-deployment.rendered}" + resource_count = 1 # this is optional } ``` diff --git a/main.go b/main.go index 19a38467..6cce443b 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,11 @@ func resourceManifest() *schema.Resource { Required: true, Sensitive: true, }, + "resource_count": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: nil, + }, }, } } @@ -164,8 +169,9 @@ func resourceManifestCreate(d *schema.ResourceData, m interface{}) error { if err := json.Unmarshal(stdout.Bytes(), &data); err != nil { return fmt.Errorf("decoding response: %v", err) } - if len(data.Items) != 1 { - return fmt.Errorf("expected to create 1 resource, got %d", len(data.Items)) + resourceCount, assertResourceCount := d.GetOk("resource_count") + if assertResourceCount && len(data.Items) != resourceCount { + return fmt.Errorf("expected to create %d resources, got %d", d.Get("resource_count"), len(data.Items)) } selflink := data.Items[0].Metadata.Selflink if selflink == "" { @@ -250,3 +256,4 @@ func resourceManifestRead(d *schema.ResourceData, m interface{}) error { } return nil } +