Skip to content

Commit

Permalink
Merge pull request #53 from squaredup/work/ss/scopes
Browse files Browse the repository at this point in the history
Support for Scopes
  • Loading branch information
shaswot77 authored Apr 22, 2024
2 parents 53fb95c + 828599d commit 41ca01e
Show file tree
Hide file tree
Showing 8 changed files with 910 additions and 0 deletions.
97 changes: 97 additions & 0 deletions docs/resources/scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "squaredup_scope Resource - terraform-provider-squaredup"
subcategory: ""
description: |-
SquaredUp Scope
---

# squaredup_scope (Resource)

SquaredUp Scope

## Example Usage

```terraform
data "squaredup_datasources" "sample_data" {
data_source_name = "Sample Data"
}
resource "squaredup_datasource" "sample_data_source" {
display_name = "Sample Data"
data_source_name = data.squaredup_datasources.sample_data.plugins[0].display_name
}
resource "squaredup_workspace" "application_workspace" {
display_name = "Application Team"
description = "Workspace with Dashboards for Application Team"
datasources_links = [squaredup_datasource.sample_data_source.id]
}
resource "squaredup_scope" "advanced_scope" {
scope_type = "advanced"
display_name = "Advanced Scope"
workspace_id = squaredup_workspace.application_workspace.id
advanced_query = "g.V().has('__configId', '${squaredup_datasource.sample_data_source.id}').has('sourceId', 'sample-server-2')" //any gremlin query
}
data "squaredup_nodes" "acommon_node" {
depends_on = [squaredup_datasource.sample_data_source]
data_source_id = squaredup_datasource.sample_data_source.id
node_name = "account-common-lambda"
}
data "squaredup_nodes" "api_node" {
depends_on = [squaredup_datasource.sample_data_source]
data_source_id = squaredup_datasource.sample_data_source.id
node_name = "master-api-lambda"
}
resource "squaredup_scope" "fixed_scope" {
scope_type = "fixed"
display_name = "Fixed Scope"
workspace_id = squaredup_workspace.application_workspace.id
node_ids = [data.squaredup_nodes.acommon_node.node_properties[0].id, data.squaredup_nodes.api_node.node_properties[0].id]
}
resource "squaredup_scope" "dynamic_scope" {
scope_type = "dynamic"
display_name = "Dynamic Scope"
workspace_id = squaredup_workspace.application_workspace.id
data_source_id = [squaredup_datasource.sample_data_source.id] //if no data source is provided, it will search within all
types = ["sample-function"] //if no type is provided, it will search within all
search_query = "account-common" //similar to search bar
}
```

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

### Required

- `display_name` (String) Display name for the scope
- `scope_type` (String) Type of the scope. Either 'dynamic' or 'fixed'
- `workspace_id` (String) ID of the workspace

### Optional

- `advanced_query` (String) Advanced query (Gremlin)
- `data_source_id` (List of String) IDs of the data sources to filter the scope
- `node_ids` (List of String) IDs of the nodes that scope will contain
- `search_query` (String) Search query
- `types` (List of String) Node types to filter the scope

### Read-Only

- `id` (String) ID of the scope
- `last_updated` (String) Last updated timestamp
- `query` (String) Query for the scope

## Import

Import is supported using the following syntax:

```shell
# Scopes can be imported by specifying workspace id and scope id
terraform import squaredup_scope.example space-123,scope-123
```
2 changes: 2 additions & 0 deletions examples/resources/squaredup_scope/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Scopes can be imported by specifying workspace id and scope id
terraform import squaredup_scope.example space-123,scope-123
49 changes: 49 additions & 0 deletions examples/resources/squaredup_scope/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
data "squaredup_datasources" "sample_data" {
data_source_name = "Sample Data"
}

resource "squaredup_datasource" "sample_data_source" {
display_name = "Sample Data"
data_source_name = data.squaredup_datasources.sample_data.plugins[0].display_name
}

resource "squaredup_workspace" "application_workspace" {
display_name = "Application Team"
description = "Workspace with Dashboards for Application Team"
datasources_links = [squaredup_datasource.sample_data_source.id]
}

resource "squaredup_scope" "advanced_scope" {
scope_type = "advanced"
display_name = "Advanced Scope"
workspace_id = squaredup_workspace.application_workspace.id
advanced_query = "g.V().has('__configId', '${squaredup_datasource.sample_data_source.id}').has('sourceId', 'sample-server-2')" //any gremlin query
}

data "squaredup_nodes" "acommon_node" {
depends_on = [squaredup_datasource.sample_data_source]
data_source_id = squaredup_datasource.sample_data_source.id
node_name = "account-common-lambda"
}

data "squaredup_nodes" "api_node" {
depends_on = [squaredup_datasource.sample_data_source]
data_source_id = squaredup_datasource.sample_data_source.id
node_name = "master-api-lambda"
}

resource "squaredup_scope" "fixed_scope" {
scope_type = "fixed"
display_name = "Fixed Scope"
workspace_id = squaredup_workspace.application_workspace.id
node_ids = [data.squaredup_nodes.acommon_node.node_properties[0].id, data.squaredup_nodes.api_node.node_properties[0].id]
}

resource "squaredup_scope" "dynamic_scope" {
scope_type = "dynamic"
display_name = "Dynamic Scope"
workspace_id = squaredup_workspace.application_workspace.id
data_source_id = [squaredup_datasource.sample_data_source.id] //if no data source is provided, it will search within all
types = ["sample-function"] //if no type is provided, it will search within all
search_query = "account-common" //similar to search bar
}
82 changes: 82 additions & 0 deletions internal/provider/client_scope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package provider

import (
"encoding/json"
"net/http"
"strings"
)

func (c *SquaredUpClient) CreateScope(scope ScopeCreate, workspaceId string) (string, error) {
rb, err := json.Marshal(scope)
if err != nil {
return "", err
}

req, err := http.NewRequest("POST", c.baseURL+"/api/workspaces/"+workspaceId+"/scopes", strings.NewReader(string(rb)))
if err != nil {
return "", err
}

body, err := c.doRequest(req)
if err != nil {
return "", err
}

scopeID := string(body)
scopeID = strings.Trim(scopeID, `"`)

return scopeID, nil
}

func (c *SquaredUpClient) GetScope(scopeId string, workspaceId string) (*ScopeRead, error) {
req, err := http.NewRequest("GET", c.baseURL+"/api/workspaces/"+workspaceId+"/scopes/"+scopeId, nil)
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}

scope := ScopeRead{}
err = json.Unmarshal(body, &scope)
if err != nil {
return nil, err
}

return &scope, nil
}

func (c *SquaredUpClient) UpdateScope(scopeId string, scope ScopeCreate, workspaceId string) error {
rb, err := json.Marshal(scope)
if err != nil {
return err
}

req, err := http.NewRequest("PUT", c.baseURL+"/api/workspaces/"+workspaceId+"/scopes/"+scopeId, strings.NewReader(string(rb)))
if err != nil {
return err
}

_, err = c.doRequest(req)
if err != nil {
return err
}

return nil
}

func (c *SquaredUpClient) DeleteScope(scopeId string, workspaceId string) error {
req, err := http.NewRequest("DELETE", c.baseURL+"/api/workspaces/"+workspaceId+"/scopes/"+scopeId, nil)
if err != nil {
return err
}

_, err = c.doRequest(req)
if err != nil {
return err
}

return nil
}
40 changes: 40 additions & 0 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,43 @@ type Script struct {
type ScriptConfig struct {
Src string `json:"src"`
}

type ScopeCreate struct {
Scope Scope `json:"scope"`
}

type Scope struct {
Name string `json:"name"`
Version int `json:"version"`
Query string `json:"query"`
Bindings map[string]interface{} `json:"bindings,omitempty"`
QueryDetail ScopeQueryDetail `json:"queryDetail"`
}

type ScopeQueryDetail struct {
IDs []string `json:"ids,omitempty"`
Plugins []ScopeQueryDetailPlugin `json:"plugins,omitempty"`
Types []ScopeQueryDetailType `json:"types,omitempty"`
BooleanQuery string `json:"booleanQuery,omitempty"`
}

type ScopeQueryDetailPlugin struct {
Value string `json:"value,omitempty"`
}

type ScopeQueryDetailType struct {
Value string `json:"value,omitempty"`
}

type ScopeRead struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
Data ScopeData `json:"data"`
WorkspaceID string `json:"workspaceId"`
}

type ScopeData struct {
ID string `json:"id"`
Query string `json:"query"`
QueryDetail string `json:"queryDetail"`
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,6 @@ func (p *squaredupProvider) Resources(_ context.Context) []func() resource.Resou
SquaredUpAlertingChannelResource,
SquaredupWorkspaceAlertResource,
SquaredUpScriptResource,
SquaredUpScopeResource,
}
}
Loading

0 comments on commit 41ca01e

Please sign in to comment.