Skip to content

Commit

Permalink
Merge pull request #25 from mohith10/doc-feature
Browse files Browse the repository at this point in the history
Added "Documentation" feature
  • Loading branch information
hughwphamill authored Sep 28, 2023
2 parents 1510d69 + 41069fe commit b7746b7
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/resources/documentation.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## Schema

### Required

- `topology_uid` (String)
- `doc_url` (String)

### Read-Only

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


5 changes: 5 additions & 0 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
24 changes: 24 additions & 0 deletions examples/resources/documentation_resource/resource.tf
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions internal/dcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
77 changes: 77 additions & 0 deletions internal/dcloud/resource_documentation.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b7746b7

Please sign in to comment.