Skip to content

Commit

Permalink
Add resource_count fixes ericchiang#4
Browse files Browse the repository at this point in the history
  • Loading branch information
gordalina committed Sep 18, 2018
1 parent 6a55723 commit 0fb3c92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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
}
```

Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func resourceManifest() *schema.Resource {
Required: true,
Sensitive: true,
},
"resource_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: nil,
},
},
}
}
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -250,3 +256,4 @@ func resourceManifestRead(d *schema.ResourceData, m interface{}) error {
}
return nil
}

0 comments on commit 0fb3c92

Please sign in to comment.