From 7397f5f9392ff938be236b794ea0823bfc8ffeac Mon Sep 17 00:00:00 2001 From: danischm Date: Tue, 18 Apr 2023 18:20:51 +0200 Subject: [PATCH 1/3] Add mso rest data source --- mso/datasource_mso_rest.go | 46 +++++++++++++++++++++++++++++++ mso/provider.go | 1 + website/docs/d/rest.html.markdown | 27 ++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 mso/datasource_mso_rest.go create mode 100644 website/docs/d/rest.html.markdown diff --git a/mso/datasource_mso_rest.go b/mso/datasource_mso_rest.go new file mode 100644 index 00000000..f0e9ae95 --- /dev/null +++ b/mso/datasource_mso_rest.go @@ -0,0 +1,46 @@ +package mso + +import ( + "log" + + "github.com/ciscoecosystem/mso-go-client/client" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" +) + +func datasourceMSORest() *schema.Resource { + return &schema.Resource{ + + Read: datasourceMSORestRead, + + SchemaVersion: version, + + Schema: (map[string]*schema.Schema{ + "path": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + }, + "content": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + }), + } +} + +func datasourceMSORestRead(d *schema.ResourceData, m interface{}) error { + log.Printf("[DEBUG] %s: Beginning Read", d.Id()) + + path := d.Get("path").(string) + msoClient := m.(*client.Client) + content, err := MakeRestRequest(msoClient, path, "GET", "{}") + if err != nil { + return err + } + d.SetId(path) + d.Set("content", content.String()) + + log.Printf("[DEBUG] %s: Read finished successfully", d.Id()) + return nil +} diff --git a/mso/provider.go b/mso/provider.go index 644cb37f..9504c73f 100644 --- a/mso/provider.go +++ b/mso/provider.go @@ -159,6 +159,7 @@ func Provider() terraform.ResourceProvider { "mso_schema_template_service_graph": dataSourceMSOSchemaTemplateServiceGraph(), "mso_service_node_type": dataSourceMSOServiceNodeType(), "mso_schema_template_contract_service_graph": datasourceTemplateContractServiceGraph(), + "mso_rest": datasourceMSORest(), }, ConfigureFunc: configureClient, diff --git a/website/docs/d/rest.html.markdown b/website/docs/d/rest.html.markdown new file mode 100644 index 00000000..4b06e1d7 --- /dev/null +++ b/website/docs/d/rest.html.markdown @@ -0,0 +1,27 @@ +--- +layout: "mso" +page_title: "MSO: mso_rest" +sidebar_current: "docs-mso-data-source-rest" +description: |- + MSO Rest data source to read MSO objects via REST API. +--- + +# mso_rest # + +MSO Rest data source to read MSO objects via REST API. + +## Example Usage ## + +```hcl +data "mso_rest" "system_config" { + path = "api/v1/platform/systemConfig" +} +``` + +## Argument Reference ## + +* `path` - (Required) MSO REST endpoint, where the data is being read. + +## Attribute Reference ## + +* `content` - (Optional) JSON response as a string. From 1c2c8360b241ba162ba7696e5dab2d9281fdce66 Mon Sep 17 00:00:00 2001 From: danischm Date: Tue, 18 Apr 2023 21:19:09 +0200 Subject: [PATCH 2/3] Make content attribute computed --- mso/datasource_mso_rest.go | 4 ++-- website/docs/d/rest.html.markdown | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mso/datasource_mso_rest.go b/mso/datasource_mso_rest.go index f0e9ae95..6438c4e3 100644 --- a/mso/datasource_mso_rest.go +++ b/mso/datasource_mso_rest.go @@ -23,14 +23,14 @@ func datasourceMSORest() *schema.Resource { }, "content": &schema.Schema{ Type: schema.TypeString, - Optional: true, + Computed: true, }, }), } } func datasourceMSORestRead(d *schema.ResourceData, m interface{}) error { - log.Printf("[DEBUG] %s: Beginning Read", d.Id()) + log.Printf("[DEBUG] Beginning Read") path := d.Get("path").(string) msoClient := m.(*client.Client) diff --git a/website/docs/d/rest.html.markdown b/website/docs/d/rest.html.markdown index 4b06e1d7..a009af49 100644 --- a/website/docs/d/rest.html.markdown +++ b/website/docs/d/rest.html.markdown @@ -24,4 +24,4 @@ data "mso_rest" "system_config" { ## Attribute Reference ## -* `content` - (Optional) JSON response as a string. +* `content` - (Read-Only) JSON response as a string. From a5ebab11e0a836c6908bbe6c6f55cdcdd71f47ac Mon Sep 17 00:00:00 2001 From: danischm Date: Wed, 19 Apr 2023 13:06:55 +0200 Subject: [PATCH 3/3] Update debug log message --- mso/datasource_mso_rest.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mso/datasource_mso_rest.go b/mso/datasource_mso_rest.go index 6438c4e3..2781c089 100644 --- a/mso/datasource_mso_rest.go +++ b/mso/datasource_mso_rest.go @@ -30,9 +30,9 @@ func datasourceMSORest() *schema.Resource { } func datasourceMSORestRead(d *schema.ResourceData, m interface{}) error { - log.Printf("[DEBUG] Beginning Read") - path := d.Get("path").(string) + log.Printf("[DEBUG] %s: Beginning Read", path) + msoClient := m.(*client.Client) content, err := MakeRestRequest(msoClient, path, "GET", "{}") if err != nil { @@ -41,6 +41,6 @@ func datasourceMSORestRead(d *schema.ResourceData, m interface{}) error { d.SetId(path) d.Set("content", content.String()) - log.Printf("[DEBUG] %s: Read finished successfully", d.Id()) + log.Printf("[DEBUG] %s: Read finished successfully", path) return nil }