diff --git a/docs/resources/documentation.md b/docs/resources/documentation.md new file mode 100644 index 0000000..61b79bf --- /dev/null +++ b/docs/resources/documentation.md @@ -0,0 +1,26 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "dcloud_topology Resource - terraform-provider-dcloud" +subcategory: "" +description: |- + +--- + +# dcloud_documentation (Resource) + + + + +## Schema + +### Required + +- `topology_uid` (String) +- `doc_url` (String) + +### Read-Only + +- `id` (String) The ID of this resource. +- `uid` (String) + + diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index 6e626bc..2975df9 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -212,4 +212,9 @@ resource "dcloud_scenario" "scenario" { internal_name = "option2" display_name = "Launch Demo Context2" } +} + +resource "dcloud_documentation" "documentation" { + topology_uid = dcloud_topology.test_topology.id + doc_url = "https://johndoe.com" } \ No newline at end of file diff --git a/examples/resources/documentation_resource/resource.tf b/examples/resources/documentation_resource/resource.tf new file mode 100644 index 0000000..6446069 --- /dev/null +++ b/examples/resources/documentation_resource/resource.tf @@ -0,0 +1,24 @@ +terraform { + required_providers { + dcloud = { + version = "0.1" + source = "cisco-open/dcloud" + } + } +} + +provider "dcloud" { + tb_url = "https://tbv3-production.ciscodcloud.com/api" +} + +resource "dcloud_topology" "test_topology" { + name = "Documentation Resource Test" + description = "Testing Topology Documentation Resource Management" + notes = "Created via Terraform Test" + datacenter = "LON" +} + +resource "dcloud_documentation" "test_documentation" { + topology_uid = dcloud_topology.test_topology.id + doc_url = "https://johndoe.com" +} \ No newline at end of file diff --git a/internal/dcloud/provider.go b/internal/dcloud/provider.go index fc224aa..6daf697 100644 --- a/internal/dcloud/provider.go +++ b/internal/dcloud/provider.go @@ -75,6 +75,7 @@ func Provider() *schema.Provider { "dcloud_hw_start_order": resourceHwStartOrder(), "dcloud_remote_access": resourceRemoteAccess(), "dcloud_scenario": resourceScenario(), + "dcloud_documentation": resourceDoc(), }, ConfigureContextFunc: providerConfigure, } diff --git a/internal/dcloud/resource_documentation.go b/internal/dcloud/resource_documentation.go new file mode 100644 index 0000000..5abdbba --- /dev/null +++ b/internal/dcloud/resource_documentation.go @@ -0,0 +1,77 @@ +package dcloud + +import ( + "context" + "github.com/cisco-open/dcloud-tb-go-client/tbclient" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceDoc() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceDocCreate, + ReadContext: resourceDocRead, + UpdateContext: resourceDocUpdate, + DeleteContext: resourceDocDelete, + Schema: map[string]*schema.Schema{ + "topology_uid": { + Type: schema.TypeString, + Required: true, + }, + "doc_url": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +/* +Documentation always exists within the context of a Topology, so instead of creating it we read the current one and update its values +*/ + +func resourceDocCreate(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { + c := i.(*tbclient.Client) + + var diags diag.Diagnostics + uid := data.Get("topology_uid").(string) + + documentation, err := c.GetDocumentation(uid) + if err != nil { + return diag.FromErr(err) + } + + documentation.DocumentationUrl = data.Get("doc_url").(string) + _, err = c.UpdateDocumentation(*documentation) + if err != nil { + return diag.FromErr(err) + } + data.SetId(documentation.Uid) + resourceDocRead(ctx, data, i) + return diags +} + +func resourceDocRead(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { + c := i.(*tbclient.Client) + + var diags diag.Diagnostics + uid := data.Get("topology_uid").(string) + + documentation, err := c.GetDocumentation(uid) + if err != nil { + return diag.FromErr(err) + } + data.Set("topology_uid", documentation.Uid) + data.Set("doc_url", documentation.DocumentationUrl) + data.SetId(documentation.Uid) + return diags +} + +func resourceDocUpdate(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { + return resourceDocCreate(ctx, data, i) +} + +func resourceDocDelete(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { + data.Set("doc_url", "") + return resourceDocCreate(ctx, data, i) +}