Skip to content

Commit

Permalink
WIP: maniputale TF stae based on FMC job history
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-bainhelixpe committed Nov 21, 2024
1 parent f9cc402 commit 27c727d
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 28 deletions.
4 changes: 2 additions & 2 deletions docs/resources/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This resource can manage a Deployment.
```terraform
resource "fmc_deployment" "example" {
version = "1457566762351"
device_list = ["d94f7ada-d141-11e5-acf3-c41f7e67fb1b"]
device_list = ["2fe9063e-8bd5-11ef-9475-e4aeac78cf37"]
}
```

Expand All @@ -25,7 +25,7 @@ resource "fmc_deployment" "example" {
### Required

- `device_list` (Set of String) List of device ids to be deployed.
- `version` (String) Epoch unix time stamp.
- `version` (String) Epoch unix time stamp (13 digits).

### Optional

Expand Down
3 changes: 3 additions & 0 deletions examples/data-sources/fmc_deployment/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "fmc_deployment" "example" {
id = "76d24097-41c4-4558-a4d0-a8c07ac08470"
}
2 changes: 1 addition & 1 deletion examples/resources/fmc_deployment/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "fmc_deployment" "example" {
version = "1457566762351"
device_list = ["d94f7ada-d141-11e5-acf3-c41f7e67fb1b"]
device_list = ["2fe9063e-8bd5-11ef-9475-e4aeac78cf37"]
}
21 changes: 18 additions & 3 deletions gen/definitions/deploy_device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,52 @@ no_update: true
no_data_source: true
no_import: true
doc_category: Deployment
test_tags: [TF_VAR_timestamp,TF_VAR_device_id_list]
attributes:
- model_name: type
type: String
value: "DeploymentRequest"
- model_name: version
type: String
description: Epoch unix time stamp.
description: Epoch unix time stamp (13 digits).
mandatory: true
example: "1457566762351"
test_value: var.timestamp
- model_name: ForceDeploy
tf_name: force_deploy
type: Bool
description: Force deployment (even if there are no configuration changes).
mandatory: false
exclude_example: true
exclude_test: true
- model_name: ignoreWarning
tf_name: ignore_warning
type: Bool
description: Ignore warnings during deployment.
mandatory: false
exclude_example: true
exclude_test: true
test_value: true
minimum_test_value: true
- model_name: deviceList
tf_name: device_list
type: Set
description: List of device ids to be deployed.
mandatory: true
element_type: String
example: d94f7ada-d141-11e5-acf3-c41f7e67fb1b
example: 2fe9063e-8bd5-11ef-9475-e4aeac78cf37
test_value: var.device_id_list
- model_name: deploymentNote
tf_name: deployment_note
type: String
description: User note related to deployment.
mandatory: false
example: "yournotescomehere"
exclude_example: true
exclude_test: true

test_prerequisites: |-
variable "timestamp" { default = null } // tests will set $TF_VAR_timestamp
variable "device_id_list" { // tests will set $TF_VAR_device_id_list
type = list(string)
default = null
}
138 changes: 138 additions & 0 deletions internal/provider/data_source_fmc_deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://mozilla.org/MPL/2.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: MPL-2.0

package provider

// Section below is generated&owned by "gen/generator.go". //template:begin imports
import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/netascode/go-fmc"
)

// End of section. //template:end imports

// Section below is generated&owned by "gen/generator.go". //template:begin model

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &DeploymentDataSource{}
_ datasource.DataSourceWithConfigure = &DeploymentDataSource{}
)

func NewDeploymentDataSource() datasource.DataSource {
return &DeploymentDataSource{}
}

type DeploymentDataSource struct {
client *fmc.Client
}

func (d *DeploymentDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_deployment"
}

func (d *DeploymentDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "This data source can read the Deployment.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The id of the object",
Required: true,
},
"domain": schema.StringAttribute{
MarkdownDescription: "The name of the FMC domain",
Optional: true,
},
"version": schema.StringAttribute{
MarkdownDescription: "Epoch unix time stamp (13 digits).",
Computed: true,
},
"force_deploy": schema.BoolAttribute{
MarkdownDescription: "Force deployment (even if there are no configuration changes).",
Computed: true,
},
"ignore_warning": schema.BoolAttribute{
MarkdownDescription: "Ignore warnings during deployment.",
Computed: true,
},
"device_list": schema.SetAttribute{
MarkdownDescription: "List of device ids to be deployed.",
ElementType: types.StringType,
Computed: true,
},
"deployment_note": schema.StringAttribute{
MarkdownDescription: "User note related to deployment.",
Computed: true,
},
},
}
}

func (d *DeploymentDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

d.client = req.ProviderData.(*FmcProviderData).Client
}

// End of section. //template:end model

// Section below is generated&owned by "gen/generator.go". //template:begin read

func (d *DeploymentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config Deployment

// Read config
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Set request domain if provided
reqMods := [](func(*fmc.Req)){}
if !config.Domain.IsNull() && config.Domain.ValueString() != "" {
reqMods = append(reqMods, fmc.DomainName(config.Domain.ValueString()))
}

tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.Id.String()))
urlPath := config.getPath() + "/" + url.QueryEscape(config.Id.ValueString())
res, err := d.client.Get(urlPath, reqMods...)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err))
return
}

config.fromBody(ctx, res)

tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.Id.ValueString()))

diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)
}

// End of section. //template:end read
80 changes: 80 additions & 0 deletions internal/provider/data_source_fmc_deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://mozilla.org/MPL/2.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: MPL-2.0

package provider

// Section below is generated&owned by "gen/generator.go". //template:begin imports
import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

// End of section. //template:end imports

// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSource

func TestAccDataSourceFmcDeployment(t *testing.T) {
if os.Getenv("TF_VAR_timestamp") == "" && os.Getenv("TF_VAR_device_id_list") == "" {
t.Skip("skipping test, set environment variable TF_VAR_timestamp or TF_VAR_device_id_list")
}
var checks []resource.TestCheckFunc
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceFmcDeploymentPrerequisitesConfig + testAccDataSourceFmcDeploymentConfig(),
Check: resource.ComposeTestCheckFunc(checks...),
},
},
})
}

// End of section. //template:end testAccDataSource

// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites

const testAccDataSourceFmcDeploymentPrerequisitesConfig = `
variable "timestamp" { default = null } // tests will set $TF_VAR_timestamp
variable "device_id_list" { // tests will set $TF_VAR_device_id_list
type = list(string)
default = null
}
`

// End of section. //template:end testPrerequisites

// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig

func testAccDataSourceFmcDeploymentConfig() string {
config := `resource "fmc_deployment" "test" {` + "\n"
config += ` version = var.timestamp` + "\n"
config += ` ignore_warning = true` + "\n"
config += ` device_list = var.device_id_list` + "\n"
config += `}` + "\n"

config += `
data "fmc_deployment" "test" {
id = fmc_deployment.test.id
}
`
return config
}

// End of section. //template:end testAccDataSourceConfig
Loading

0 comments on commit 27c727d

Please sign in to comment.