Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable tests to be ran in the pipeline, add documentation #16

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ lint-fix: $(GOLANGCI_LINT)

.PHONY: test
test:
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
TF_ACC=1 TF_ACC_PROVIDER_NAMESPACE=cisco-open go test ./... -v $(TESTARGS) -timeout 120m

.PHONY: plugin
plugin:
Expand Down
11 changes: 11 additions & 0 deletions docs/data-sources/type.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ description: |-

Type data source

Enables you to read any type of Cisco Observability Platform. Types are just schemas for the objects.
Type name is the field required to get the type data source.

## Example usage

In this example we want to fetch a type called fmm:namespace.

```terraform
data "observability_type" "ns" {
type_name = "fmm:namespace"
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
41 changes: 40 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,50 @@ description: |-

---

# observability Provider
# Observability Provider

Terraform Observability provider enables users to provision objects as resources and read types as data sources

## Authentication

To start using the Observability Terraform Provider you need to authenticate with the Cisco Observability Platform.

You can do this by either using service-principal or oauth as an authentication method:


```terraform
terraform {
required_providers {
observability = {
source = "registry.terraform.io/cisco-open/observability"
}
}
}

provider "observability" {
tenant = "<your cisco observability account>"
auth_method = "service-principal"
url = "https://<your environment/host>"
secrets_file = "<path to your secrets file>"
}

```

```terraform
terraform {
required_providers {
observability = {
source = "registry.terraform.io/cisco-open/observability"
}
}
}

provider "observability" {
tenant = "<your cisco observability account>"
auth_method = "oauth"
url = "https://<your environment/host>"
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
21 changes: 20 additions & 1 deletion docs/resources/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,26 @@ description: |-

Object resource


Enables you to create and manage any object of Cisco Observability Platform. Objects are actual populated values of a type. Data is a field which acts like a container for any object payload.

## Example usage

```terraform
resource "observability_object" "conn" {
type_name = "<your type>"
object_id = "<object id of the object>"
layer_type = "TENANT"
layer_id = "<your tenant>"
import_id = "<object type>|<object id>|TENANT|<your tenant>"
data = jsonencode(
{
"field1" : "value1",
"field2" : "value2",
...
}
)
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
23 changes: 21 additions & 2 deletions internal/provider/object_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *ObjectResource) Create(ctx context.Context, req resource.CreateRequest,
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

//nolint:gocritic // Terraform framework requires the method signature to be as is
//nolint:gocritic,funlen // Terraform framework requires the method signature to be as is
func (r *ObjectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
tflog.Debug(ctx, "Read method invoked")
var data ObjectResourceModel
Expand Down Expand Up @@ -179,6 +179,13 @@ func (r *ObjectResource) Read(ctx context.Context, req resource.ReadRequest, res
layerID = identityFields[3]
}

tflog.Debug(ctx, fmt.Sprintf("type name is %s", typeName))
tflog.Debug(ctx, fmt.Sprintf("object id is %s", objID))
tflog.Debug(ctx, fmt.Sprintf("layer ID is %s", layerID))
tflog.Debug(ctx, fmt.Sprintf("layer type %s", layerType))
tflog.Debug(ctx, fmt.Sprintf("data payload %s", currentDataPayload))
tflog.Debug(ctx, fmt.Sprintf("import identifier is %s", importIdentifier))

result, err := r.client.GetObject(typeName, objID, layerID, layerType)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -188,6 +195,7 @@ func (r *ObjectResource) Read(ctx context.Context, req resource.ReadRequest, res
return
}

tflog.Debug(ctx, fmt.Sprintf("api response is %s", string(result)))
// update the model with the new values
var parsedCurrentDataPayload map[string]any
var parsedResponse map[string]any
Expand All @@ -201,6 +209,7 @@ func (r *ObjectResource) Read(ctx context.Context, req resource.ReadRequest, res
return
}

tflog.Debug(ctx, fmt.Sprintf("parsed response into map is %v", parsedResponse))
if currentDataPayload != "" {
err = json.Unmarshal([]byte(currentDataPayload), &parsedCurrentDataPayload)
if err != nil {
Expand All @@ -212,7 +221,17 @@ func (r *ObjectResource) Read(ctx context.Context, req resource.ReadRequest, res
}
}

dataPayload := parsedResponse["data"].(map[string]any)
// if we can't fetch any data from the cloud return
var dataPayload map[string]any
var ok bool
if dataPayload, ok = parsedResponse["data"].(map[string]any); !ok {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to assert data map from current object of type %s with id %s", typeName, objID),
err.Error(),
)
return
}

if parsedCurrentDataPayload == nil {
// data was not provided in this case, maybe import usecase
// populate all the fields with what the observability api provided
Expand Down
15 changes: 7 additions & 8 deletions internal/provider/object_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

//lint:ignore U1000 Ignore unused function temporarily for debugging
func _TestAccObjectResource(t *testing.T) {
func TestAccObjectResource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -59,12 +58,12 @@ resource "observability_object" "test" {
),
},
// ImportState testing
// TO DO add this after the plugin is plublished
// {
// ResourceName: "observability_object.test",
// ImportState: true,
// ImportStateVerify: true,
// },
{
ResourceName: "observability_object.test",
ImportState: true,
ImportStateVerify: true,
ImportStateId: "anzen:cloudConnection|just-terraform-testing|TENANT|0eb4e853-34fb-4f77-b3fc-b9cd3b462366",
},
// Update and Read testing
{
Config: providerConfig + `
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
terraform {
required_providers {
observability = {
source = "testTerraform.com/appd/observability"
source = "registry.terraform.io/cisco-open/observability",
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/type_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

//lint:ignore U1000 Ignore unused function temporarily for debugging
func _TestAccTypeDataSource(t *testing.T) {
func TestAccTypeDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
Expand Down
Loading