Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
Added node import
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
terricain committed Apr 23, 2020
1 parent c808e66 commit 3aaa23a
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ website/node_modules
*.iml

website/vendor
key.pem

# Test exclusions
!command/test-fixtures/**/*.tfstate
Expand Down
28 changes: 28 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Setting up Chef Zero

Run Chef:

```
mkdir /tmp/chef
# For persistance
docker run --rm -p 8889:8889 --name chef -v /tmp/chef:/work -it rubygem/chef-zero -H 0.0.0.0 -p 8889 -l debug --file-store /work
# Without persistance
docker run --rm -p 8889:8889 --name chef -it rubygem/chef-zero -H 0.0.0.0 -p 8889 -l debug
```

Setup Environment:

Generate a private key to use in the key material var

```
openssl genrsa -out key.pem 2048
export CHEF_SERVER_URL=http://127.0.0.1:8889/
export CHEF_CLIENT_NAME=test
export CHEF_KEY_MATERIAL="$(cat key.pem)"
```

Run:

```
make testacc
```
8 changes: 4 additions & 4 deletions chef/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"server_url": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("CHEF_SERVER_URL", nil),
Description: "URL of the root of the target Chef server or organization.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("CHEF_SERVER_URL", nil),
Description: "URL of the root of the target Chef server or organization.",
ValidateFunc: validateServerURL,
},
"client_name": {
Expand Down
13 changes: 10 additions & 3 deletions chef/resource_data_bag.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package chef

import (
"github.com/hashicorp/terraform/helper/schema"

chefc "github.com/go-chef/chef"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceChefDataBag() *schema.Resource {
Expand Down Expand Up @@ -58,7 +57,7 @@ func ReadDataBag(d *schema.ResourceData, meta interface{}) error {

name := d.Id()

_, err := client.DataBags.ListItems(name)
dataBagList, err := client.DataBags.List()
if err != nil {
if errRes, ok := err.(*chefc.ErrorResponse); ok {
if errRes.Response.StatusCode == 404 {
Expand All @@ -68,7 +67,15 @@ func ReadDataBag(d *schema.ResourceData, meta interface{}) error {
}
}

apiURL, ok := (*dataBagList)[name]
if !ok { // Not found
d.SetId("")
return nil
}

d.Set("name", name)
d.Set("api_uri", apiURL)

return err
}

Expand Down
1 change: 0 additions & 1 deletion chef/resource_data_bag_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package chef
import (
"encoding/json"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/schema"
Expand Down
10 changes: 8 additions & 2 deletions chef/resource_data_bag_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func TestAccDataBagItem_basic(t *testing.T) {
"chef_data_bag_item.test", &dataBagItemName,
),
},
{
ResourceName: "chef_data_bag_item.test",
// Import ID needs to be bagname.item_id
ImportStateId: "terraform-acc-test-bag-item-basic.terraform_acc_test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -83,8 +90,7 @@ resource "chef_data_bag" "test" {
name = "terraform-acc-test-bag-item-basic"
}
resource "chef_data_bag_item" "test" {
data_bag_name = "terraform-acc-test-bag-item-basic"
depends_on = ["chef_data_bag.test"]
data_bag_name = chef_data_bag.test.name
content_json = <<EOT
{
"id": "terraform_acc_test",
Expand Down
5 changes: 5 additions & 0 deletions chef/resource_data_bag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func TestAccDataBag_basic(t *testing.T) {
Config: testAccDataBagConfig_basic,
Check: testAccDataBagCheckExists("chef_data_bag.test", &dataBagName),
},
{
ResourceName: "chef_data_bag.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
7 changes: 4 additions & 3 deletions chef/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package chef
import (
"encoding/json"
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"

Expand Down Expand Up @@ -35,11 +34,13 @@ func resourceChefEnvironment() *schema.Resource {
"default_attributes_json": {
Type: schema.TypeString,
Optional: true,
Default: "{}",
StateFunc: jsonStateFunc,
},
"override_attributes_json": {
Type: schema.TypeString,
Optional: true,
Default: "{}",
StateFunc: jsonStateFunc,
},
"cookbook_constraints": {
Expand Down Expand Up @@ -114,13 +115,13 @@ func ReadEnvironment(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
d.Set("default_attributes_json", defaultAttrJSON)
d.Set("default_attributes_json", string(defaultAttrJSON))

overrideAttrJSON, err := json.Marshal(env.OverrideAttributes)
if err != nil {
return err
}
d.Set("override_attributes_json", overrideAttrJSON)
d.Set("override_attributes_json", string(overrideAttrJSON))

cookbookVersionsI := map[string]interface{}{}
for k, v := range env.CookbookVersions {
Expand Down
20 changes: 20 additions & 0 deletions chef/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func TestAccEnvironment_basic(t *testing.T) {
},
),
},
{
ResourceName: "chef_environment.test",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccEnvironmentConfig_basic2,
ResourceName: "chef_environment.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -118,3 +129,12 @@ EOT
}
}
`
const testAccEnvironmentConfig_basic2 = `
resource "chef_environment" "test" {
name = "terraform-acc-test-basic"
description = "Terraform Acceptance Tests"
cookbook_constraints = {
"terraform" = "= 1.0.0"
}
}
`
24 changes: 16 additions & 8 deletions chef/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func resourceChefNode() *schema.Resource {
Read: ReadNode,
Delete: DeleteNode,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -63,6 +67,7 @@ func resourceChefNode() *schema.Resource {
}
}

// CreateNode Creates a Chef Node from the resource definition
func CreateNode(d *schema.ResourceData, meta interface{}) error {
client := meta.(*chefc.Client)

Expand All @@ -80,6 +85,7 @@ func CreateNode(d *schema.ResourceData, meta interface{}) error {
return ReadNode(d, meta)
}

// UpdateNode Updates a Chef Node to match the resource definition
func UpdateNode(d *schema.ResourceData, meta interface{}) error {
client := meta.(*chefc.Client)

Expand All @@ -97,6 +103,7 @@ func UpdateNode(d *schema.ResourceData, meta interface{}) error {
return ReadNode(d, meta)
}

// ReadNode Updates the resource object with data retrieved from Chef
func ReadNode(d *schema.ResourceData, meta interface{}) error {
client := meta.(*chefc.Client)

Expand All @@ -117,29 +124,29 @@ func ReadNode(d *schema.ResourceData, meta interface{}) error {
d.Set("name", node.Name)
d.Set("environment_name", node.Environment)

automaticAttrJson, err := json.Marshal(node.AutomaticAttributes)
automaticAttrJSON, err := json.Marshal(node.AutomaticAttributes)
if err != nil {
return err
}
d.Set("automatic_attributes_json", automaticAttrJson)
d.Set("automatic_attributes_json", string(automaticAttrJSON))

normalAttrJson, err := json.Marshal(node.NormalAttributes)
normalAttrJSON, err := json.Marshal(node.NormalAttributes)
if err != nil {
return err
}
d.Set("normal_attributes_json", normalAttrJson)
d.Set("normal_attributes_json", string(normalAttrJSON))

defaultAttrJson, err := json.Marshal(node.DefaultAttributes)
defaultAttrJSON, err := json.Marshal(node.DefaultAttributes)
if err != nil {
return err
}
d.Set("default_attributes_json", defaultAttrJson)
d.Set("default_attributes_json", string(defaultAttrJSON))

overrideAttrJson, err := json.Marshal(node.OverrideAttributes)
overrideAttrJSON, err := json.Marshal(node.OverrideAttributes)
if err != nil {
return err
}
d.Set("override_attributes_json", overrideAttrJson)
d.Set("override_attributes_json", string(overrideAttrJSON))

runListI := make([]interface{}, len(node.RunList))
for i, v := range node.RunList {
Expand All @@ -150,6 +157,7 @@ func ReadNode(d *schema.ResourceData, meta interface{}) error {
return nil
}

// DeleteNode Deletes a Chef Node
func DeleteNode(d *schema.ResourceData, meta interface{}) error {
client := meta.(*chefc.Client)

Expand Down
7 changes: 6 additions & 1 deletion chef/resource_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func TestAccNode_basic(t *testing.T) {
},
),
},
{
ResourceName: "chef_node.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -113,7 +118,7 @@ resource "chef_environment" "test" {
}
resource "chef_node" "test" {
name = "terraform-acc-test-basic"
environment_name = "terraform-acc-test-node-basic"
environment_name = chef_environment.test.name
automatic_attributes_json = <<EOT
{
"terraform_acc_test": true
Expand Down
8 changes: 5 additions & 3 deletions chef/resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ func resourceChefRole() *schema.Resource {
"default_attributes_json": {
Type: schema.TypeString,
Optional: true,
Default: "{}",
StateFunc: jsonStateFunc,
},
"override_attributes_json": {
Type: schema.TypeString,
Optional: true,
Default: "{}",
StateFunc: jsonStateFunc,
},
"run_list": {
Expand Down Expand Up @@ -115,13 +117,13 @@ func ReadRole(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
d.Set("default_attributes_json", defaultAttrJSON)
d.Set("default_attributes_json", string(defaultAttrJSON))

overrideAttrJSON, err := json.Marshal(role.OverrideAttributes)
if err != nil {
return err
}
d.Set("override_attributes_json", overrideAttrJSON)
d.Set("override_attributes_json", string(overrideAttrJSON))

runListI := make([]interface{}, len(role.RunList))
for i, v := range role.RunList {
Expand All @@ -132,7 +134,7 @@ func ReadRole(d *schema.ResourceData, meta interface{}) error {
return nil
}

// Delete Chef role
// DeleteRole Delete Chef role
func DeleteRole(d *schema.ResourceData, meta interface{}) error {
client := meta.(*chefc.Client)

Expand Down
5 changes: 5 additions & 0 deletions chef/resource_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func TestAccRole_basic(t *testing.T) {
},
),
},
{
ResourceName: "chef_role.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ description: |-
framework. The Chef provider allows Terraform to manage various resources
that exist within [Chef Server](http://docs.chef.io/chef_server.html).

-> **Important:** The Chef server URL must end with a slash.

Use the navigation to the left to read about the available resources.

## Example Usage
Expand Down
14 changes: 13 additions & 1 deletion website/docs/r/data_bag_item.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ data bag itself, use the ``chef_data_bag`` resource.
## Example Usage

```hcl
resource "chef_data_bag" "somebag" {
name = "somebag"
}
resource "chef_data_bag_item" "example" {
data_bag_name = "example-data-bag"
data_bag_name = chef_data_bag.somebag.name
content_json = <<EOT
{
Expand Down Expand Up @@ -47,3 +51,11 @@ The following attributes are exported:

* `id` - The value of the "id" property in the ``content_json`` JSON object,
which can be used by clients to retrieve this item's content.

## Import

Data Bag Items can be imported using `data_bag_name.item_name`, e.g.

```
$ terraform import chef_data_bag_item.example somebag.itemname
```
Loading

0 comments on commit 3aaa23a

Please sign in to comment.