Skip to content

Commit

Permalink
Add service resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Castrianni authored and Shawn Castrianni committed Jun 7, 2023
1 parent 5a6b851 commit 8addd77
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 25 deletions.
8 changes: 4 additions & 4 deletions docs/data-sources/role.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Represents a role
## Example usage
```hcl
data "konnect_role" "example" {
group_display_name = "Runtime Groups"
entity_type_display_name = "Runtime Groups"
display_name = "Admin"
}
```
## Argument Reference
* `group_display_name` - **(Required, String)** The display name of the role group. Must be `Runtime Groups` or `Services`
* `entity_type_display_name` - **(Required, String)** The display name of the role entity type. Must be `Runtime Groups` or `Services`
* `display_name` - **(Required, String)** The display name of the Role. Uses equality.
## Attribute Reference
* `id` - **(String)** Same as `group_name`:`name`
* `group_name` - **(String)** The name of the role group.
* `id` - **(String)** Same as `entity_type_name`:`name`
* `entity_type_name` - **(String)** The name of the role entity type.
* `name` - **(String)** The name of the role.
* `description` - **(String)** The description of the role.
2 changes: 1 addition & 1 deletion docs/data-sources/team_role.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data "konnect_team_role" "example" {
* `search_role_display_name` - **(Optional, String)** The search string to apply to the display name of the role. Uses contains.
* `role_display_name` - **(Optional, String)** The filter string to apply to the display name of the role. Uses equality.
* `search_entity_type_display_name` - **(Optional, String)** The search string to apply to the display name of the entity type, like `Runtime Groups` or `Services`. Uses contains.
* `entity_type_display_name` - **(Optional, String)** The filter string to apply to the display name of the entity type, like `Runtime Groups` or `Services`. Uses equality.
* `entity_type_display_name` - **(Optional, String)** The filter string to apply to the display name of the entity type. Must be `Runtime Groups` or `Services`. Uses equality.
## Attribute Reference
* `id` - **(String)** Same as `team_id`:`Guid of role assignment`
* `entity_id` - **(String)** The id of the entity for which the role applies.
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/user_role.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data "konnect_user_role" "example" {
* `search_role_display_name` - **(Optional, String)** The search string to apply to the display name of the role. Uses contains.
* `role_display_name` - **(Optional, String)** The filter string to apply to the display name of the role. Uses equality.
* `search_entity_type_display_name` - **(Optional, String)** The search string to apply to the display name of the entity type, like `Runtime Groups` or `Services`. Uses contains.
* `entity_type_display_name` - **(Optional, String)** The filter string to apply to the display name of the entity type, like `Runtime Groups` or `Services`. Uses equality.
* `entity_type_display_name` - **(Optional, String)** The filter string to apply to the display name of the entity type. Must be `Runtime Groups` or `Services`. Uses equality.
## Attribute Reference
* `id` - **(String)** Same as `user_id`:`Guid of role assignment`
* `entity_id` - **(String)** The id of the entity for which the role applies.
Expand Down
32 changes: 32 additions & 0 deletions docs/resources/service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
subcategory: "Runtime Configuration"
---
# Resource: konnect_service
Represents a service within a runtime group
## Example usage
```hcl
data "konnect_runtime_group" "RuntimeGroup" {
name = "TestRuntimeGroup"
}
resource "konnect_service" "example" {
runtime_group_id = data.konnect_runtime_group.RuntimeGroup.id
host = "mockbin.org"
name = "Test"
}
```
## Argument Reference
* `runtime_group_id` - **(Required, String)** The id of the runtime group.
* `host` - **(Required, String)** The host of the service.
* `name` - **(Optional, String)** The name of the service.
* `retries` - **(Optional, Integer)** The number of retries to execute upon failure to proxy. Default: `5`
* `protocol` - **(Optional, String)** The protocol used to communicate with the host. Allowed values: `grpc`, `grpcs`, `http`, `https`, `tcp`, `tls`, `tls_passthrough`, `udp`, `ws`, `wss`
* `port` - **(Optional, Integer)** The port used to communicate with the host. Default: `80`
* `path` - **(Optional, String)** The path to be used in requests to the host.
* `connect_timeout` - **(Optional, Integer)** The timeout in milliseconds for establishing a connection to the host. Default: `60000`
* `read_timeout` - **(Optional, Integer)** The timeout in milliseconds between two successive read operations for transmitting a request to the host. Default: `60000`
* `write_timeout` - **(Optional, Integer)** The timeout in milliseconds between two successive write operations for transmitting a request to the host. Default: `60000`
* `enabled` - **(Optional, Boolean)** Whether the Service is active. Default: `true`
## Attribute Reference
* `id` - **(String)** Same as `runtime_group_id`:`id`
## Import
Services can be imported using a proper value of `id` as described above
35 changes: 35 additions & 0 deletions konnect/client/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import "strings"

const (
ServicePath = RuntimeGroupPathGet + "/core-entities/services"
ServicePathGet = ServicePath + "/%s"
)

type Service struct {
RuntimeGroupId string `json:"-"`
Id string `json:"id"`
Name string `json:"name"`
Retries int `json:"retries"`
Protocol string `json:"protocol"`
Host string `json:"host"`
Port int `json:"port"`
Path string `json:"path"`
ConnectTimeout int `json:"connect_timeout"`
ReadTimeout int `json:"read_timeout"`
WriteTimeout int `json:"write_timeout"`
Enabled bool `json:"enabled"`
}
type ServiceCollection struct {
Services []Service `json:"data"`
}

func (s *Service) ServiceEncodeId() string {
return s.RuntimeGroupId + IdSeparator + s.Id
}

func ServiceDecodeId(s string) (string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1]
}
10 changes: 5 additions & 5 deletions konnect/data_source_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func dataSourceRole() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceRoleRead,
Schema: map[string]*schema.Schema{
"group_display_name": {
"entity_type_display_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{client.RuntimeGroupsDisplayName, client.ServicesDisplayName}, false),
Expand All @@ -25,7 +25,7 @@ func dataSourceRole() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"group_name": {
"entity_type_name": {
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -44,7 +44,7 @@ func dataSourceRole() *schema.Resource {
func dataSourceRoleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*client.Client)
groupDisplayName := d.Get("group_display_name").(string)
entityTypeDisplayName := d.Get("entity_type_display_name").(string)
displayName := d.Get("display_name").(string)
body, err := c.HttpRequest(ctx, false, http.MethodGet, client.RolePath, nil, nil, &bytes.Buffer{})
if err != nil {
Expand All @@ -62,7 +62,7 @@ func dataSourceRoleRead(ctx context.Context, d *schema.ResourceData, m interface
foundGroup = nil
foundGroupName := ""
for k, g := range *retVals {
if g.DisplayName == groupDisplayName {
if g.DisplayName == entityTypeDisplayName {
foundGroup = &g
foundGroupName = k
break
Expand All @@ -87,7 +87,7 @@ func dataSourceRoleRead(ctx context.Context, d *schema.ResourceData, m interface
d.SetId("")
return diag.FromErr(fmt.Errorf("No role exists with that filter criteria"))
}
d.Set("group_name", foundGroupName)
d.Set("entity_type_name", foundGroupName)
d.Set("name", foundRoleName)
d.Set("description", foundRole.Description)
d.SetId(foundGroupName + client.IdSeparator + foundRoleName)
Expand Down
6 changes: 4 additions & 2 deletions konnect/data_source_team_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scastria/terraform-provider-konnect/konnect/client"
"net/http"
"net/url"
Expand All @@ -33,8 +34,9 @@ func dataSourceTeamRole() *schema.Resource {
Optional: true,
},
"entity_type_display_name": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{client.RuntimeGroupsDisplayName, client.ServicesDisplayName}, false),
},
"entity_id": {
Type: schema.TypeString,
Expand Down
7 changes: 5 additions & 2 deletions konnect/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scastria/terraform-provider-konnect/konnect/client"
"net/http"
"net/url"
"regexp"
"strconv"
)

Expand All @@ -22,8 +24,9 @@ func dataSourceUser() *schema.Resource {
Optional: true,
},
"email": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[^\s@]+@[^\s@]+\.[^\s@]+$`), "must be a valid email address"),
},
"search_full_name": {
Type: schema.TypeString,
Expand Down
6 changes: 4 additions & 2 deletions konnect/data_source_user_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scastria/terraform-provider-konnect/konnect/client"
"net/http"
"net/url"
Expand All @@ -33,8 +34,9 @@ func dataSourceUserRole() *schema.Resource {
Optional: true,
},
"entity_type_display_name": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{client.RuntimeGroupsDisplayName, client.ServicesDisplayName}, false),
},
"entity_id": {
Type: schema.TypeString,
Expand Down
9 changes: 6 additions & 3 deletions konnect/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scastria/terraform-provider-konnect/konnect/client"
)

Expand All @@ -16,9 +17,10 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("KONNECT_PAT", nil),
},
"region": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KONNECT_REGION", "us"),
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KONNECT_REGION", "us"),
ValidateFunc: validation.StringInSlice([]string{"us", "eu"}, false),
},
},
ResourcesMap: map[string]*schema.Resource{
Expand All @@ -30,6 +32,7 @@ func Provider() *schema.Provider {
"konnect_team_user": resourceTeamUser(),
"konnect_team_role": resourceTeamRole(),
"konnect_user_role": resourceUserRole(),
"konnect_service": resourceService(),
},
DataSourcesMap: map[string]*schema.Resource{
"konnect_runtime_group": dataSourceRuntimeGroup(),
Expand Down
Loading

0 comments on commit 8addd77

Please sign in to comment.