From d2ddeaaa5e2d6e84d1582136fb5b9c624b3b1b1e Mon Sep 17 00:00:00 2001 From: nleiva Date: Thu, 28 Apr 2022 23:55:32 -0400 Subject: [PATCH] Add test cases --- GNUmakefile | 2 +- .../provider/data_source_manufacturers.go | 13 +++--- .../data_source_manufacturers_test.go | 43 +++++++++---------- internal/provider/resource_manufacturer.go | 10 ++--- .../provider/resource_manufacturer_test.go | 12 ++++-- 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 3bfd890..92cb9ba 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -3,7 +3,7 @@ HOSTNAME=github.com NAMESPACE=nleiva NAME=nautobot BINARY=terraform-provider-${NAME} -VERSION=0.2.0 +VERSION=0.2.1 OS_ARCH=$(shell go env GOOS)_$(shell go env GOARCH) diff --git a/internal/provider/data_source_manufacturers.go b/internal/provider/data_source_manufacturers.go index 84a9550..9c476c5 100644 --- a/internal/provider/data_source_manufacturers.go +++ b/internal/provider/data_source_manufacturers.go @@ -31,16 +31,16 @@ func dataSourceManufacturers() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "custom_fields": { - Description: "Manufacturer custom fields.", - Type: schema.TypeMap, - Optional: true, - }, "description": { Description: "Manufacturer's description.", Type: schema.TypeString, Optional: true, }, + "custom_fields": { + Description: "Manufacturer custom fields.", + Type: schema.TypeMap, + Optional: true, + }, "devicetype_count": { Description: "Manufacturer's device count.", Type: schema.TypeInt, @@ -50,6 +50,7 @@ func dataSourceManufacturers() *schema.Resource { Description: "Manufacturer's display name.", Type: schema.TypeString, Optional: true, + Computed: true, }, "id": { Description: "Manufacturer's UUID.", @@ -80,11 +81,13 @@ func dataSourceManufacturers() *schema.Resource { Description: "Manufacturer's slug.", Type: schema.TypeString, Optional: true, + Computed: true, }, "url": { Description: "Manufacturer's URL.", Type: schema.TypeString, Optional: true, + Computed: true, }, }, }, diff --git a/internal/provider/data_source_manufacturers_test.go b/internal/provider/data_source_manufacturers_test.go index e3b3f32..271547f 100644 --- a/internal/provider/data_source_manufacturers_test.go +++ b/internal/provider/data_source_manufacturers_test.go @@ -1,13 +1,13 @@ package provider import ( - "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccDataSourceManufacturers(t *testing.T) { + // https://github.com/hashicorp/terraform-plugin-sdk/issues/952 t.Skip("resource not yet implemented, remove this once you add your own code") resource.UnitTest(t, resource.TestCase{ @@ -17,15 +17,8 @@ func TestAccDataSourceManufacturers(t *testing.T) { { Config: testAccDataSourceManufacturer, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "data.nautobot_manufacturers.juniper", "name", "Juniper"), - ), - }, - { - Config: testAccDataSourceManufacturer, - Check: resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr( - "data.nautobot_manufacturers.juniper", "id", regexp.MustCompile("^4873d752")), + //resource.TestCheckResourceAttr("data.nautobot_manufacturers.list.manufacturers", "", ""), + resource.TestCheckOutput("vendor", "juniper"), ), }, }, @@ -33,17 +26,23 @@ func TestAccDataSourceManufacturers(t *testing.T) { } const testAccDataSourceManufacturer = ` -resource "nautobot_manufacturer" "juniper" { - id = "4873d752-5dbe-4006-8345-8279a0dfbbda" - url = "https://develop.demo.nautobot.com/api/dcim/manufacturers/4873d752-5dbe-4006-8345-8279a0dfbbda/" - name = "Juniper" - slug = "juniper" - description = "Juniper Networks" - devicetype_count = 0 - platform_count = 1 - custom_fields = "{}" - created = "2022-03-08" - last_updated = "2022-03-08T14:50:48.492203Z" - display = "Juniper" +provider "nautobot" { + url = "https://demo.nautobot.com/api/" + token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + } + +data "nautobot_manufacturers" "list" {} + +variable "filter" { + type = string + default = "Juniper" +} + +output "vendor" { + value = [ + for manufacturer in data.nautobot_manufacturers.list.manufacturers : + manufacturer.slug + if manufacturer.name == var.filter + ] } ` diff --git a/internal/provider/resource_manufacturer.go b/internal/provider/resource_manufacturer.go index 4d056e1..9d02f72 100644 --- a/internal/provider/resource_manufacturer.go +++ b/internal/provider/resource_manufacturer.go @@ -29,16 +29,16 @@ func resourceManufacturer() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "custom_fields": { - Description: "Manufacturer custom fields.", - Type: schema.TypeMap, - Optional: true, - }, "description": { Description: "Manufacturer's description.", Type: schema.TypeString, Optional: true, }, + "custom_fields": { + Description: "Manufacturer custom fields.", + Type: schema.TypeMap, + Optional: true, + }, "devicetype_count": { Description: "Manufacturer's device count.", Type: schema.TypeInt, diff --git a/internal/provider/resource_manufacturer_test.go b/internal/provider/resource_manufacturer_test.go index 2ce6955..da73203 100644 --- a/internal/provider/resource_manufacturer_test.go +++ b/internal/provider/resource_manufacturer_test.go @@ -18,15 +18,21 @@ func TestAccResourceManufacturer(t *testing.T) { Config: testAccResourceManufacturer, Check: resource.ComposeTestCheckFunc( resource.TestMatchResourceAttr( - "nautobot_manufacturer.foo", "sample_attribute", regexp.MustCompile("^ba")), + "nautobot_manufacturer.test", "slug", regexp.MustCompile("^juniper$")), ), }, }, }) + } const testAccResourceManufacturer = ` -resource "nautobot_manufacturer" "foo" { - sample_attribute = "bar" +provider "nautobot" { + url = "https://demo.nautobot.com/api/" + token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + } + +resource "nautobot_manufacturer" "test" { + name = "Juniper" } `