Skip to content

Commit

Permalink
support github actions (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedwigz authored Sep 4, 2022
1 parent ae1cd8b commit b2179b1
Show file tree
Hide file tree
Showing 8 changed files with 581 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ HOSTNAME=github.com
NAMESPACE=port-labs
NAME=port-labs
BINARY=terraform-provider-${NAME}
VERSION=0.3.1
VERSION=0.4.0
OS=$(shell go env GOOS)
ARCH=$(shell go env GOARCH)
OS_ARCH=${OS}_${ARCH}
Expand Down
53 changes: 53 additions & 0 deletions docs/resources/action.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "port-labs_action Resource - terraform-provider-port-labs"
subcategory: ""
description: |-
Port action
---

# port-labs_action (Resource)

Port action



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `blueprint_identifier` (String) The identifier of the blueprint
- `identifier` (String) The identifier of the action
- `invocation_method` (String) The methods the action is dispatched in, currently only supports KAFKA
- `title` (String) The display name of the action
- `trigger` (String) The type of the action, one of CREATE, DAY-2, DELETE

### Optional

- `description` (String) The description of the action
- `icon` (String) The icon of the action
- `required_properties` (Set of String) The required properties of the action
- `user_properties` (Block Set) The input properties of the action (see [below for nested schema](#nestedblock--user_properties))

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedblock--user_properties"></a>
### Nested Schema for `user_properties`

Required:

- `identifier` (String) The identifier of the property
- `title` (String) A nicely written name for the property
- `type` (String) The type of the property

Optional:

- `default` (String) A default value for this property in case an entity is created without explicitly providing a value.
- `description` (String) A description of the property. This value is visible to users when hovering on the info icon in the UI. It provides detailed information about the use of a specific property.
- `format` (String) A specific data format to pair with some of the available types
- `pattern` (String) A regular expression (regex) pattern to specify the set of allowed values for the property


17 changes: 17 additions & 0 deletions examples/resources/port-labs_action/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "port-labs_action" "restart_microservice" {
title = "Restart microservice"
icon = "Terraform"
identifier = "restart-micrservice"
blueprint_identifier = port-labs_blueprint.microservice.identifier
trigger = "DAY-2"
invocation_method = "KAFKA"
user_properties {
identifier = "webhook_url"
type = "string"
title = "Webhook URL"
description = "Webhook URL to send the request to"
format = "url"
default = "https://example.com"
pattern = "^https://.*"
}
}
91 changes: 91 additions & 0 deletions port/cli/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cli

import (
"context"
"encoding/json"
"fmt"
)

func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*Action, error) {
pb := &PortBody{}
url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}"
resp, err := c.Client.R().
SetContext(ctx).
SetHeader("Accept", "application/json").
SetResult(pb).
SetPathParam("blueprint_identifier", blueprintID).
SetPathParam("action_identifier", id).
Get(url)
if err != nil {
return nil, err
}
if !pb.OK {
return nil, fmt.Errorf("failed to read action, got: %s", resp.Body())
}
return &pb.Action, nil
}

func (c *PortClient) CreateAction(ctx context.Context, blueprintID string, action *Action) (*Action, error) {
url := "v1/blueprints/{blueprint_identifier}/actions"
resp, err := c.Client.R().
SetBody(action).
SetPathParam("blueprint_identifier", blueprintID).
SetContext(ctx).
Post(url)
if err != nil {
return nil, err
}
var pb PortBody
err = json.Unmarshal(resp.Body(), &pb)
if err != nil {
return nil, err
}
if !pb.OK {
return nil, fmt.Errorf("failed to create action, got: %s", resp.Body())
}
return &pb.Action, nil
}

func (c *PortClient) UpdateAction(ctx context.Context, blueprintID, actionID string, action *Action) (*Action, error) {
url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}"
resp, err := c.Client.R().
SetBody(action).
SetContext(ctx).
SetPathParam("blueprint_identifier", blueprintID).
SetPathParam("action_identifier", actionID).
Put(url)
if err != nil {
return nil, err
}
var pb PortBody
err = json.Unmarshal(resp.Body(), &pb)
if err != nil {
return nil, err
}
if !pb.OK {
return nil, fmt.Errorf("failed to create action, got: %s", resp.Body())
}
return &pb.Action, nil
}

func (c *PortClient) DeleteAction(ctx context.Context, blueprintID, actionID string) error {
url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}"
resp, err := c.Client.R().
SetContext(ctx).
SetHeader("Accept", "application/json").
SetPathParam("blueprint_identifier", blueprintID).
SetPathParam("action_identifier", actionID).
Delete(url)
if err != nil {
return err
}
responseBody := make(map[string]interface{})
err = json.Unmarshal(resp.Body(), &responseBody)
if err != nil {
return err
}
if !(responseBody["ok"].(bool)) {
return fmt.Errorf("failed to delete action. got:\n%s", string(resp.Body()))
}
return nil
}
17 changes: 16 additions & 1 deletion port/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,33 @@ type (
Default string `json:"default,omitempty"`
Format string `json:"format,omitempty"`
Description string `json:"description,omitempty"`
Pattern string `json:"pattern,omitempty"`
}

BlueprintSchema struct {
Properties map[string]BlueprintProperty `json:"properties"`
Required []string `json:"required,omitempty"`
}

ActionUserInputs = BlueprintSchema

Blueprint struct {
Meta
Identifier string `json:"identifier,omitempty"`
Title string `json:"title"`
Icon string `json:"icon"`
Schema BlueprintSchema `json:"schema"`
// TODO: relations
}

Action struct {
ID string `json:"id,omitempty"`
Identifier string `json:"identifier,omitempty"`
Description string `json:"description,omitempty"`
Title string `json:"title,omitempty"`
Icon string `json:"icon,omitempty"`
UserInputs ActionUserInputs `json:"userInputs"`
Trigger string `json:"trigger"`
InvocationMethod string `json:"invocationMethod"`
}

Relation struct {
Expand All @@ -62,4 +76,5 @@ type PortBody struct {
OK bool `json:"ok"`
Entity Entity `json:"entity"`
Blueprint Blueprint `json:"blueprint"`
Action Action `json:"action"`
}
1 change: 1 addition & 0 deletions port/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"port-labs_entity": newEntityResource(),
"port-labs_blueprint": newBlueprintResource(),
"port-labs_action": newActionResource(),
},
DataSourcesMap: map[string]*schema.Resource{},
ConfigureContextFunc: providerConfigure,
Expand Down
Loading

0 comments on commit b2179b1

Please sign in to comment.