Skip to content

Commit

Permalink
Jz/add functionality labels (#138)
Browse files Browse the repository at this point in the history
* add labels to functionality

* add labels to functionality data source

* add labels to tests

* stop using default priorities in tests

* update changelog

* use dynamic priority name
  • Loading branch information
Jeff Zellner authored Nov 2, 2023
1 parent b006b82 commit c5bff90
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.3.7 (Unreleased)

* Add labels to functionalities

## 0.3.6

* Fix client versioning
Expand Down
35 changes: 0 additions & 35 deletions firehydrant/functionalities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package firehydrant

import (
"context"
"time"

"github.com/dghubble/sling"
"github.com/pkg/errors"
Expand All @@ -27,23 +26,6 @@ func (c *RESTFunctionalitiesClient) restClient() *sling.Sling {
return c.client.client()
}

// FunctionalityResponse is the payload for a single environment
// URL: GET https://api.firehydrant.io/v1/functionalities/{id}
type FunctionalityResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Slug string `json:"slug"`
Services []FunctionalityService `json:"services"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

// FunctionalityService represents a service when creating a functionality
type FunctionalityService struct {
ID string `json:"id"`
}

// Get retrieves a functionality from FireHydrant
func (c *RESTFunctionalitiesClient) Get(ctx context.Context, id string) (*FunctionalityResponse, error) {
funcResponse := &FunctionalityResponse{}
Expand All @@ -61,14 +43,6 @@ func (c *RESTFunctionalitiesClient) Get(ctx context.Context, id string) (*Functi
return funcResponse, nil
}

// CreateFunctionalityRequest is the payload for creating a service
// URL: POST https://api.firehydrant.io/v1/services
type CreateFunctionalityRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Services []FunctionalityService `json:"services,omitempty"`
}

// Create creates a functionality in FireHydrant
func (c *RESTFunctionalitiesClient) Create(ctx context.Context, req CreateFunctionalityRequest) (*FunctionalityResponse, error) {
funcResponse := &FunctionalityResponse{}
Expand All @@ -86,15 +60,6 @@ func (c *RESTFunctionalitiesClient) Create(ctx context.Context, req CreateFuncti
return funcResponse, nil
}

// UpdateFunctionalityRequest is the payload for updating a environment
// URL: PATCH https://api.firehydrant.io/v1/environments/{id}
type UpdateFunctionalityRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description"`
RemoveRemainingServices bool `json:"remove_remaining_services"`
Services []FunctionalityService `json:"services"`
}

// Update updates a functionality in FireHydrant
func (c *RESTFunctionalitiesClient) Update(ctx context.Context, id string, req UpdateFunctionalityRequest) (*FunctionalityResponse, error) {
funcResponse := &FunctionalityResponse{}
Expand Down
37 changes: 37 additions & 0 deletions firehydrant/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,40 @@ type Pagination struct {
Prev int `json:"prev,omitempty"`
Next int `json:"next,omitempty"`
}

// FunctionalityResponse is the payload for a single environment
// URL: GET https://api.firehydrant.io/v1/functionalities/{id}
type FunctionalityResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Slug string `json:"slug"`
Services []FunctionalityService `json:"services"`
Labels map[string]string `json:"labels"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

// FunctionalityService represents a service when creating a functionality
type FunctionalityService struct {
ID string `json:"id"`
}

// CreateFunctionalityRequest is the payload for creating a service
// URL: POST https://api.firehydrant.io/v1/services
type CreateFunctionalityRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Services []FunctionalityService `json:"services,omitempty"`
Labels map[string]string `json:"labels"`
}

// UpdateFunctionalityRequest is the payload for updating a environment
// URL: PATCH https://api.firehydrant.io/v1/environments/{id}
type UpdateFunctionalityRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description"`
RemoveRemainingServices bool `json:"remove_remaining_services"`
Labels map[string]string `json:"labels"`
Services []FunctionalityService `json:"services"`
}
5 changes: 5 additions & 0 deletions provider/functionality_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func dataSourceFunctionality() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Computed: true,
},
"service_ids": {
Type: schema.TypeSet,
Computed: true,
Expand Down Expand Up @@ -59,6 +63,7 @@ func dataFireHydrantFunctionality(ctx context.Context, d *schema.ResourceData, m
attributes := map[string]interface{}{
"name": functionalityResponse.Name,
"description": functionalityResponse.Description,
"labels": functionalityResponse.Labels,
}

serviceIDs := make([]string, 0)
Expand Down
3 changes: 3 additions & 0 deletions provider/functionality_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ resource "firehydrant_functionality" "test_functionality" {
name = "test-functionality-%s"
description = "test-description-%s"
service_ids = [firehydrant_service.test_service.id]
labels = {
test1 = "test-label1-foo",
}
}
data "firehydrant_functionality" "test_functionality" {
Expand Down
7 changes: 7 additions & 0 deletions provider/functionality_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func resourceFunctionality() *schema.Resource {
Type: schema.TypeString,
},
},
"labels": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -69,6 +73,7 @@ func readResourceFireHydrantFunctionality(ctx context.Context, d *schema.Resourc
attributes := map[string]interface{}{
"name": functionalityResponse.Name,
"description": functionalityResponse.Description,
"labels": functionalityResponse.Labels,
}

serviceIDs := make([]string, 0)
Expand All @@ -95,6 +100,7 @@ func createResourceFireHydrantFunctionality(ctx context.Context, d *schema.Resou
createRequest := firehydrant.CreateFunctionalityRequest{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Labels: convertStringMap(d.Get("labels").(map[string]interface{})),
}

// Process any optional attributes and add to the create request if necessary
Expand Down Expand Up @@ -129,6 +135,7 @@ func updateResourceFireHydrantFunctionality(ctx context.Context, d *schema.Resou
updateRequest := firehydrant.UpdateFunctionalityRequest{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Labels: convertStringMap(d.Get("labels").(map[string]interface{})),
}

// Process any optional attributes and add to the update request if necessary
Expand Down
6 changes: 6 additions & 0 deletions provider/functionality_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ func testAccFunctionalityResourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "firehydrant_functionality" "test_functionality" {
name = "test-functionality-%s"
labels = {
test1 = "test-label1-foo",
}
}`, rName)
}

Expand All @@ -245,6 +248,9 @@ resource "firehydrant_service" "test_service2" {
resource "firehydrant_functionality" "test_functionality" {
name = "test-functionality-%s"
description = "test-description-%s"
labels = {
test1 = "test-label1-foo",
}
service_ids = [
firehydrant_service.test_service1.id,
Expand Down
20 changes: 12 additions & 8 deletions provider/priority_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,45 @@ package provider

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccPriorityDataSource_basic(t *testing.T) {
rSlug := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccPriorityDataSourceConfig_basic(),
Config: testAccPriorityDataSourceConfig_basic(rSlug),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.firehydrant_priority.test_priority", "id"),
resource.TestCheckResourceAttr(
"data.firehydrant_priority.test_priority", "slug", "TESTPRIORITY"),
"data.firehydrant_priority.test_priority", "slug", fmt.Sprintf("TESTPRIORITY%s", rSlug)),
resource.TestCheckResourceAttr(
"data.firehydrant_priority.test_priority", "description", "test-description"),
resource.TestCheckResourceAttr(
"data.firehydrant_priority.test_priority", "default", "true"),
"data.firehydrant_priority.test_priority", "default", "false"),
),
},
},
})
}

func testAccPriorityDataSourceConfig_basic() string {
return fmt.Sprintln(`
func testAccPriorityDataSourceConfig_basic(rSlug string) string {
return fmt.Sprintf(`
resource "firehydrant_priority" "test_priority" {
slug = "TESTPRIORITY"
slug = "TESTPRIORITY%s"
description = "test-description"
default = true
default = false
}
data "firehydrant_priority" "test_priority" {
slug = firehydrant_priority.test_priority.id
}`)
}`, rSlug)
}
4 changes: 2 additions & 2 deletions provider/priority_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestAccPriorityResource_update(t *testing.T) {
resource.TestCheckResourceAttr(
"firehydrant_priority.test_priority", "description", fmt.Sprintf("test-description-%s", rSlugUpdated)),
resource.TestCheckResourceAttr(
"firehydrant_priority.test_priority", "default", "true"),
"firehydrant_priority.test_priority", "default", "false"),
),
},
{
Expand Down Expand Up @@ -258,7 +258,7 @@ func testAccPriorityResourceConfig_update(rSlug string) string {
resource "firehydrant_priority" "test_priority" {
slug = "TESTPRIORITY%s"
description = "test-description-%s"
default = true
default = false
}`, rSlug, rSlug)
}

Expand Down

0 comments on commit c5bff90

Please sign in to comment.