Skip to content

Commit

Permalink
[minor_change] Add mso_schema_site_l3out resource and datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
akinross committed Oct 17, 2024
1 parent 5f1db4e commit 82cc380
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/schema_site_l3out/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
terraform {
required_providers {
mso = {
source = "CiscoDevNet/mso"
}
}
}

provider "mso" {
username = "" # <MSO username>
password = "" # <MSO pwd>
url = "" # <MSO URL>
insecure = true
}

data "mso_site" "example" {
name = "example"
}

data "mso_tenant" "example" {
name = "example"
display_name = "example"
}

resource "mso_schema" "example" {
name = "example"
template_name = "example"
tenant_id = data.mso_tenant.example.id
}

resource "mso_schema_template_vrf" "vrf" {
schema_id = mso_schema.example.id
template = mso_schema.example.template_name
name = "example"
}

resource "mso_schema_template_l3out" "l3out" {
schema_id = mso_schema.example.id
template_name = mso_schema.example.template_name
l3out_name = "example"
vrf_name = mso_schema_template_vrf.vrf.id
vrf_schema_id = mso_schema_template_vrf.vrf.schema_id
vrf_template_name = mso_schema_template_vrf.vrf.template
}


resource "mso_schema_site" "example" {
schema_id = mso_schema.example.id
site_id = data.mso_site.example.id
template_name = "example"
}

resource "mso_schema_site_vrf" "example" {
template_name = mso_schema_site.example.template_name
site_id = mso_schema_site.example.site_id
schema_id = mso_schema_site.example.schema_id
vrf_name = mso_schema_template_vrf.example.name
}

resource "mso_schema_site_l3out" "example" {
schema_id = mso_schema_site.example.schema_id
l3out_name = mso_schema_template_l3out.l3out.l3out_name
template_name = mso_schema_site.example.template_name
vrf_name = mso_schema_site_vrf.example.vrf_name
site_id = mso_schema_site.example.site_id
}
70 changes: 70 additions & 0 deletions mso/datasource_mso_schema_site_l3out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package mso

import (
"log"

"github.com/ciscoecosystem/mso-go-client/client"
"github.com/ciscoecosystem/mso-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func datasourceMSOSchemaSiteL3out() *schema.Resource {
return &schema.Resource{
Read: datasourceMSOSchemaSiteL3outRead,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"l3out_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"vrf_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"template_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"site_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"schema_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
},
}
}

func datasourceMSOSchemaSiteL3outRead(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] Schema Site L3out: Beginning Read")
msoClient := m.(*client.Client)
schemaId := d.Get("schema_id").(string)
siteId := d.Get("site_id").(string)
templateName := d.Get("template_name").(string)
vrfName := d.Get("vrf_name").(string)
l3outName := d.Get("l3out_name").(string)
l3outMap := models.IntersiteL3outs{
SchemaID: schemaId,
SiteId: siteId,
TemplateName: templateName,
VRFName: vrfName,
L3outName: l3outName,
}
l3outMapRemote, err := msoClient.ReadIntersiteL3outs(&l3outMap)
if err != nil {
d.SetId("")
return err
}
setMSOSchemaSiteL3outAttributes(l3outMapRemote, d)
d.SetId(L3outModelToL3outId(&l3outMap))
log.Println("[DEBUG] Schema Site L3out: Reading Completed", d.Id())
return nil
}
2 changes: 2 additions & 0 deletions mso/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func Provider() terraform.ResourceProvider {
"mso_schema_site_vrf_route_leak": resourceMSOSchemaSiteVrfRouteLeak(),
"mso_schema_site_vrf_region": resourceMSOSchemaSiteVrfRegion(),
"mso_schema_site_bd_subnet": resourceMSOSchemaSiteBdSubnet(),
"mso_schema_site_l3out": resourceMSOSchemaSiteL3out(),
"mso_rest": resourceMSORest(),
"mso_schema_template_deploy": resourceMSOSchemaTemplateDeploy(),
"mso_schema_template_deploy_ndo": resourceNDOSchemaTemplateDeploy(),
Expand Down Expand Up @@ -155,6 +156,7 @@ func Provider() terraform.ResourceProvider {
"mso_schema_site_vrf_region": dataSourceMSOSchemaSiteVrfRegion(),
"mso_schema_site_vrf_route_leak": dataSourceMSOSchemaSiteVrfRouteLeak(),
"mso_schema_site_bd_subnet": dataSourceMSOSchemaSiteBdSubnet(),
"mso_schema_site_l3out": datasourceMSOSchemaSiteL3out(),
"mso_schema_site_vrf_region_cidr_subnet": dataSourceMSOSchemaSiteVrfRegionCidrSubnet(),
"mso_schema_site_vrf_region_cidr": dataSourceMSOSchemaSiteVrfRegionCidr(),
"mso_schema_template_anp_epg_selector": datasourceMSOSchemaTemplateAnpEpgSelector(),
Expand Down
162 changes: 162 additions & 0 deletions mso/resource_mso_schema_site_l3out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package mso

import (
"fmt"
"log"
"strings"

"github.com/ciscoecosystem/mso-go-client/client"
"github.com/ciscoecosystem/mso-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func resourceMSOSchemaSiteL3out() *schema.Resource {
return &schema.Resource{
Create: resourceMSOSchemaSiteL3outCreate,
Read: resourceMSOSchemaSiteL3outRead,
Delete: resourceMSOSchemaSiteL3outDelete,
Importer: &schema.ResourceImporter{
State: resourceMSOSchemaSiteL3outImport,
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"l3out_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"vrf_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"template_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"site_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"schema_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
},
}
}

func setMSOSchemaSiteL3outAttributes(l3outMap *models.IntersiteL3outs, d *schema.ResourceData) {
d.Set("l3out_name", l3outMap.L3outName)
d.Set("vrf_name", l3outMap.VRFName)
d.Set("template_name", l3outMap.TemplateName)
d.Set("site_id", l3outMap.SiteId)
d.Set("schema_id", l3outMap.SchemaID)
}

func resourceMSOSchemaSiteL3outImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
log.Println("[DEBUG] Schema Site L3out: Beginning Import", d.Id())
msoClient := m.(*client.Client)
id := d.Id()
L3out, err := L3outIdToL3outModel(id)
if err != nil {
return nil, err
}
l3outMapRemote, err := msoClient.ReadIntersiteL3outs(L3out)
if err != nil {
return nil, err
}
setMSOSchemaSiteL3outAttributes(l3outMapRemote, d)
d.SetId(id)
log.Println("[DEBUG] Schema Site L3out: Import Completed", d.Id())
return []*schema.ResourceData{d}, nil
}

func resourceMSOSchemaSiteL3outCreate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] Schema Site L3out: Beginning Creation")
msoClient := m.(*client.Client)
schemaId := d.Get("schema_id").(string)
siteId := d.Get("site_id").(string)
templateName := d.Get("template_name").(string)
vrfName := d.Get("vrf_name").(string)
l3outName := d.Get("l3out_name").(string)
l3outMap := models.IntersiteL3outs{
L3outName: l3outName,
VRFName: vrfName,
SiteId: siteId,
TemplateName: templateName,
SchemaID: schemaId,
}
err := msoClient.CreateIntersiteL3outs(&l3outMap)
if err != nil {
return err
}
l3outId := L3outModelToL3outId(&l3outMap)
d.SetId(l3outId)
log.Printf("[DEBUG] Schema Site L3out: Creation Completed")
return resourceMSOSchemaSiteL3outRead(d, m)
}

func resourceMSOSchemaSiteL3outRead(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] Schema Site L3out: Beginning Read", d.Id())
msoClient := m.(*client.Client)
id := d.Id()
l3out, err := L3outIdToL3outModel(id)
if err != nil {
return err
}
l3outMapRemote, err := msoClient.ReadIntersiteL3outs(l3out)
if err != nil {
d.SetId("")
return nil
}
setMSOSchemaSiteL3outAttributes(l3outMapRemote, d)
d.SetId(L3outModelToL3outId(l3out))
log.Println("[DEBUG] Schema Site L3out: Reading Completed", d.Id())
return nil
}

func resourceMSOSchemaSiteL3outDelete(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] Schema Site L3out: Beginning Destroy", d.Id())
msoClient := m.(*client.Client)
id := d.Id()
l3out, err := L3outIdToL3outModel(id)
if err != nil {
return err
}
err = msoClient.DeleteIntersiteL3outs(l3out)
if err != nil {
return err
}
log.Println("[DEBUG] Schema Site L3out: Beginning Destroy", d.Id())
d.SetId("")
return err
}

func L3outModelToL3outId(m *models.IntersiteL3outs) string {
return fmt.Sprintf("%s/site/%s/template/%s/vrf/%s/l3out/%s", m.SchemaID, m.SiteId, m.TemplateName, m.VRFName, m.L3outName)
}

func L3outIdToL3outModel(id string) (*models.IntersiteL3outs, error) {
getAttributes := strings.Split(id, "/")
if len(getAttributes) != 9 || getAttributes[1] != "site" || getAttributes[3] != "template" || getAttributes[5] != "vrf" || getAttributes[7] != "l3out" {
return nil, fmt.Errorf("invalid mso_schema_site_l3out id format")
}
l3outMap := models.IntersiteL3outs{
SchemaID: getAttributes[0],
SiteId: getAttributes[2],
TemplateName: getAttributes[4],
VRFName: getAttributes[6],
L3outName: getAttributes[8],
}
return &l3outMap, nil
}
34 changes: 34 additions & 0 deletions website/docs/d/schema_site_l3out.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
layout: "mso"
page_title: "MSO: mso_schema_site_l3out"
sidebar_current: "docs-mso-data-source-schema_site_l3out"
description: |-
Data source for MSO Schema Site L3out
---

# mso_schema_site_l3out #

Data source for MSO schema site L3out, to fetch the MSO schema site L3out details.

## Example Usage ##

```hcl
data "mso_schema_site_l3out" "exmple" {
vrf_name = mso_schema_site_vrf.example.vrf_name
l3out_name = mso_schema_site_l3out.example.l3out_name
template_name = mso_site.example.template_name
site_id = mso_site.example.site_id
schema_id = mso_site.example.schema_id
}
```

## Argument Reference ##
* `schema_id` - (Required) The schema-id where L3out is added.
* `l3out_name` - (Required) Name of the added L3out.
* `template_name` - (Required) Template name associated with the L3out.
* `vrf_name` - (Required) VRF name associated with the L3out.
* `site_id` - (Required) SiteID associated with the L3out.

## Attribute Reference ##

No attributes are exported.
42 changes: 42 additions & 0 deletions website/docs/r/schema_site_l3out.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
layout: "mso"
page_title: "MSO: mso_schema_site_l3out"
sidebar_current: "docs-mso-resource-schema_site_l3out"
description: |-
Manages MSO Schema Site L3out
---

# mso_schema_site_l3out #

Manages MSO Schema Site L3out.

## Example Usage ##

```hcl
resource "mso_schema_site_l3out" "example" {
vrf_name = mso_schema_site_vrf.example.vrf_name
l3out_name = "example"
template_name = mso_site.example.template_name
site_id = mso_site.example.site_id
schema_id = mso_site.example.schema_id
}
```

## Argument Reference ##
* `schema_id` - (Required) The schema-id where user wants to add L3out.
* `l3out_name` - (Required) Name of the L3out that user wants to add.
* `template_name` - (Required) Template name associated with the L3out.
* `vrf_name` - (Required) VRF name associated with the L3out.
* `site_id` - (Required) SiteID associated with the L3out.

## Attribute Reference ##
The only Attribute exposed for this resource is `id`. Which is set to the node name of Service Node created.

## Importing ##

An existing MSO Schema Site L3out can be [imported][docs-import] into this resource via its Id/path, via the following command: [docs-import]: <https://www.terraform.io/docs/import/index.html>

```bash
terraform import mso_schema_site_l3out.example {schema_id}/site/{site_id}/template/{template_name}/vrf/{vrf_name}/l3out/{l3out_name}
```

0 comments on commit 82cc380

Please sign in to comment.