From 40f5e9fc0784bb0fb88624be1105341e5cca1ab6 Mon Sep 17 00:00:00 2001 From: Travis Leeden Date: Tue, 7 May 2024 14:28:15 +1000 Subject: [PATCH] Added data source for agent --- .../kubernetes_agent_deployment_targets.md | 86 +++++++++++++++++++ .../kubernetes_agent_deployment_target.md | 7 +- .../data-source.tf | 16 ++++ ...rce_kubernetes_agent_deployment_targets.go | 56 ++++++++++++ octopusdeploy/provider.go | 1 + ...hema_kubernetes_agent_deployment_target.go | 57 ++++++++++++ 6 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 docs/data-sources/kubernetes_agent_deployment_targets.md create mode 100644 examples/data-sources/octopusdeploy_kubernetes_agent_deployment_targets/data-source.tf create mode 100644 octopusdeploy/data_source_kubernetes_agent_deployment_targets.go diff --git a/docs/data-sources/kubernetes_agent_deployment_targets.md b/docs/data-sources/kubernetes_agent_deployment_targets.md new file mode 100644 index 000000000..ac5ec617e --- /dev/null +++ b/docs/data-sources/kubernetes_agent_deployment_targets.md @@ -0,0 +1,86 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "octopusdeploy_kubernetes_agent_deployment_targets Data Source - terraform-provider-octopusdeploy" +subcategory: "" +description: |- + Provides information about existing kubernetes agent deployment targets. +--- + +# octopusdeploy_kubernetes_agent_deployment_targets (Data Source) + +Provides information about existing kubernetes agent deployment targets. + +## Example Usage + +```terraform +data "octopusdeploy_kubernetes_agent_deployment_targets" "kubernetes_agent_deployment_targets" { + deployment_id = "Deployments-123" + environments = ["Environments-123", "Environments-321"] + health_statuses = ["HasWarnings"] + ids = ["Machines-123", "Machines-321"] + is_disabled = false + name = "Kubernetes Agent" + partial_name = "Kubernetes Age" + roles = ["Roles-123", "Roles-321"] + shell_names = [] + skip = 5 + take = 100 + tenant_tags = ["TagSet1/Tag"] + tenants = ["Tenants-123"] + thumbprint = "96203ED84246201C26A2F4360D7CBC36AC1D232D" +} +``` + + +## Schema + +### Optional + +- `deployment_id` (String) A filter to search by deployment ID. +- `environments` (List of String) A filter to search by a list of environment IDs. +- `health_statuses` (List of String) A filter to search by a list of health statuses of resources. Valid health statuses are `HasWarnings`, `Healthy`, `Unavailable`, `Unhealthy`, or `Unknown`. +- `ids` (List of String) A filter to search by a list of IDs. +- `is_disabled` (Boolean) A filter to search by the disabled status of a resource. +- `name` (String) A filter to search by name. +- `partial_name` (String) A filter to search by the partial match of a name. +- `roles` (List of String) A filter to search by a list of role IDs. +- `shell_names` (List of String) A list of shell names to match in the query and/or search +- `skip` (Number) A filter to specify the number of items to skip in the response. +- `space_id` (String) The space ID associated with this resource. +- `take` (Number) A filter to specify the number of items to take (or return) in the response. +- `tenant_tags` (List of String) A filter to search by a list of tenant tags. +- `tenants` (List of String) A filter to search by a list of tenant IDs. +- `thumbprint` (String) The thumbprint of the deployment target to match in the query and/or search + +### Read-Only + +- `id` (String) An auto-generated identifier that includes the timestamp when this data source was last modified. +- `kubernetes_agent_deployment_targets` (Block List) A list of kubernetes agent deployment targets that match the filter(s). (see [below for nested schema](#nestedblock--kubernetes_agent_deployment_targets)) + + +### Nested Schema for `kubernetes_agent_deployment_targets` + +Read-Only: + +- `agent_helm_release_name` (String) Name of the Helm release that the agent belongs to. +- `agent_kubernetes_namespace` (String) Name of the Kubernetes namespace where the agent is installed. +- `agent_tentacle_version` (String) Current Tentacle version of the agent +- `agent_upgrade_status` (String) Current upgrade availability status of the agent. One of 'NoUpgrades', 'UpgradeAvailable', 'UpgradeSuggested', 'UpgradeRequired' +- `agent_version` (String) Current Helm chart version of the agent. +- `communication_mode` (String) The communication mode used by the Kubernetes agent to communicate with Octopus Server. Currently, the only supported value is 'Polling'. +- `default_namespace` (String) Optional default namespace that will be used when using Kubernetes deployment steps, can be overrides within step configurations. +- `environments` (List of String) A list of environment IDs this Kubernetes agent can deploy to. +- `id` (String) The unique ID for this resource. +- `is_disabled` (Boolean) Whether the Kubernetes agent is disabled. If the agent is disabled, it will not be included in any deployments. +- `machine_policy_id` (String) Optional ID of the machine policy that the Kubernetes agent will use. If not provided the default machine policy will be used. +- `name` (String) The name of this resource. +- `roles` (List of String) A list of target roles that are associated to this Kubernetes agent. +- `space_id` (String) The space ID associated with this resource. +- `tenant_tags` (List of String) A list of tenant tags associated with this resource. +- `tenanted_deployment_participation` (String) The tenanted deployment mode of the resource. Valid account types are `Untenanted`, `TenantedOrUntenanted`, or `Tenanted`. +- `tenants` (List of String) A list of tenant IDs associated with this resource. +- `thumbprint` (String) The thumbprint of the Kubernetes agent's certificate used by server to verify the identity of the agent. This is optional as a new installation of the agent will update this value when registers with the server. +- `upgrade_locked` (Boolean) If enabled the Kubernetes agent will not automatically upgrade and will stay on the currently installed version, even if the associated machine policy is configured to automatically upgrade. +- `uri` (String) The URI of the Kubernetes agent's used by the server to queue messages. This is optional as a new installation of the agent will update this value when registers with the server. + + diff --git a/docs/resources/kubernetes_agent_deployment_target.md b/docs/resources/kubernetes_agent_deployment_target.md index 0b1a97a2b..6fff38732 100644 --- a/docs/resources/kubernetes_agent_deployment_target.md +++ b/docs/resources/kubernetes_agent_deployment_target.md @@ -58,6 +58,7 @@ resource "octopusdeploy_kubernetes_agent_deployment_target" "tenanted_agent" { - `communication_mode` (String) The communication mode used by the Kubernetes agent to communicate with Octopus Server. Currently, the only supported value is 'Polling'. - `default_namespace` (String) Optional default namespace that will be used when using Kubernetes deployment steps, can be overrides within step configurations. +- `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Whether the Kubernetes agent is disabled. If the agent is disabled, it will not be included in any deployments. - `machine_policy_id` (String) Optional ID of the machine policy that the Kubernetes agent will use. If not provided the default machine policy will be used. - `space_id` (String) The space ID associated with this resource. @@ -70,7 +71,11 @@ resource "octopusdeploy_kubernetes_agent_deployment_target" "tenanted_agent" { ### Read-Only -- `id` (String) The ID of this resource. +- `agent_helm_release_name` (String) Name of the Helm release that the agent belongs to. +- `agent_kubernetes_namespace` (String) Name of the Kubernetes namespace where the agent is installed. +- `agent_tentacle_version` (String) Current Tentacle version of the agent +- `agent_upgrade_status` (String) Current upgrade availability status of the agent. One of 'NoUpgrades', 'UpgradeAvailable', 'UpgradeSuggested', 'UpgradeRequired' +- `agent_version` (String) Current Helm chart version of the agent. ## Import diff --git a/examples/data-sources/octopusdeploy_kubernetes_agent_deployment_targets/data-source.tf b/examples/data-sources/octopusdeploy_kubernetes_agent_deployment_targets/data-source.tf new file mode 100644 index 000000000..521270a57 --- /dev/null +++ b/examples/data-sources/octopusdeploy_kubernetes_agent_deployment_targets/data-source.tf @@ -0,0 +1,16 @@ +data "octopusdeploy_kubernetes_agent_deployment_targets" "kubernetes_agent_deployment_targets" { + deployment_id = "Deployments-123" + environments = ["Environments-123", "Environments-321"] + health_statuses = ["HasWarnings"] + ids = ["Machines-123", "Machines-321"] + is_disabled = false + name = "Kubernetes Agent" + partial_name = "Kubernetes Age" + roles = ["Roles-123", "Roles-321"] + shell_names = [] + skip = 5 + take = 100 + tenant_tags = ["TagSet1/Tag"] + tenants = ["Tenants-123"] + thumbprint = "96203ED84246201C26A2F4360D7CBC36AC1D232D" +} diff --git a/octopusdeploy/data_source_kubernetes_agent_deployment_targets.go b/octopusdeploy/data_source_kubernetes_agent_deployment_targets.go new file mode 100644 index 000000000..78c598efa --- /dev/null +++ b/octopusdeploy/data_source_kubernetes_agent_deployment_targets.go @@ -0,0 +1,56 @@ +package octopusdeploy + +import ( + "context" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "time" +) + +func dataSourceKubernetesAgentDeploymentTargets() *schema.Resource { + return &schema.Resource{ + Description: "Provides information about existing kubernetes agent deployment targets.", + ReadContext: dataSourceKubernetesAgentDeploymentTargetsRead, + Schema: getKubernetesAgentDeploymentTargetDataSchema(), + } +} + +func dataSourceKubernetesAgentDeploymentTargetsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + query := machines.MachinesQuery{ + CommunicationStyles: []string{"KubernetesTentacle"}, + DeploymentID: d.Get("deployment_id").(string), + EnvironmentIDs: expandArray(d.Get("environments").([]interface{})), + HealthStatuses: expandArray(d.Get("health_statuses").([]interface{})), + IDs: expandArray(d.Get("ids").([]interface{})), + IsDisabled: d.Get("is_disabled").(bool), + Name: d.Get("name").(string), + PartialName: d.Get("partial_name").(string), + Roles: expandArray(d.Get("roles").([]interface{})), + ShellNames: expandArray(d.Get("shell_names").([]interface{})), + Skip: d.Get("skip").(int), + Take: d.Get("take").(int), + TenantIDs: expandArray(d.Get("tenants").([]interface{})), + TenantTags: expandArray(d.Get("tenant_tags").([]interface{})), + Thumbprint: d.Get("thumbprint").(string), + } + + client := m.(*client.Client) + existingDeploymentTargets, err := machines.Get(client, d.Get("space_id").(string), query) + if err != nil { + return diag.FromErr(err) + } + + flattenedKubernetesAgents := []interface{}{} + for _, deploymentTarget := range existingDeploymentTargets.Items { + flattenedKubernetesAgents = append(flattenedKubernetesAgents, flattenKubernetesAgentDeploymentTarget(deploymentTarget)) + } + + err = d.Set("kubernetes_agent_deployment_targets", flattenedKubernetesAgents) + if err != nil { + return diag.FromErr(err) + } + d.SetId("KubernetesAgentDeploymentTargets " + time.Now().UTC().String()) + return nil +} diff --git a/octopusdeploy/provider.go b/octopusdeploy/provider.go index ff425919e..8a70e04d1 100644 --- a/octopusdeploy/provider.go +++ b/octopusdeploy/provider.go @@ -21,6 +21,7 @@ func Provider() *schema.Provider { "octopusdeploy_environments": dataSourceEnvironments(), "octopusdeploy_feeds": dataSourceFeeds(), "octopusdeploy_git_credentials": dataSourceGitCredentials(), + "octopusdeploy_kubernetes_agent_deployment_targets": dataSourceKubernetesAgentDeploymentTargets(), "octopusdeploy_kubernetes_cluster_deployment_targets": dataSourceKubernetesClusterDeploymentTargets(), "octopusdeploy_library_variable_sets": dataSourceLibraryVariableSet(), "octopusdeploy_lifecycles": dataSourceLifecycles(), diff --git a/octopusdeploy/schema_kubernetes_agent_deployment_target.go b/octopusdeploy/schema_kubernetes_agent_deployment_target.go index 30098d696..be4e191ec 100644 --- a/octopusdeploy/schema_kubernetes_agent_deployment_target.go +++ b/octopusdeploy/schema_kubernetes_agent_deployment_target.go @@ -60,6 +60,8 @@ func flattenKubernetesAgentDeploymentTarget(deploymentTarget *machines.Deploymen flattenedDeploymentTarget["id"] = deploymentTarget.GetID() flattenedDeploymentTarget["space_id"] = deploymentTarget.SpaceID flattenedDeploymentTarget["name"] = deploymentTarget.Name + flattenedDeploymentTarget["environments"] = deploymentTarget.EnvironmentIDs + flattenedDeploymentTarget["roles"] = deploymentTarget.Roles flattenedDeploymentTarget["machine_policy_id"] = deploymentTarget.MachinePolicyID flattenedDeploymentTarget["is_disabled"] = deploymentTarget.IsDisabled flattenedDeploymentTarget["tenanted_deployment_participation"] = deploymentTarget.TenantedDeploymentMode @@ -71,11 +73,20 @@ func flattenKubernetesAgentDeploymentTarget(deploymentTarget *machines.Deploymen flattenedDeploymentTarget["communication_mode"] = endpoint.TentacleEndpointConfiguration.CommunicationMode flattenedDeploymentTarget["default_namespace"] = endpoint.DefaultNamespace + if endpoint.KubernetesAgentDetails != nil { + flattenedDeploymentTarget["agent_version"] = endpoint.KubernetesAgentDetails.AgentVersion + flattenedDeploymentTarget["tentacle_version"] = endpoint.KubernetesAgentDetails.TentacleVersion + flattenedDeploymentTarget["upgrade_status"] = endpoint.KubernetesAgentDetails.UpgradeStatus + flattenedDeploymentTarget["helm_release_name"] = endpoint.KubernetesAgentDetails.HelmReleaseName + flattenedDeploymentTarget["kubernetes_namespace"] = endpoint.KubernetesAgentDetails.KubernetesNamespace + } + return flattenedDeploymentTarget } func getKubernetesAgentDeploymentTargetSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ + "id": getIDSchema(), "space_id": getSpaceIDSchema(), "name": getNameSchema(true), "environments": { @@ -137,5 +148,51 @@ func getKubernetesAgentDeploymentTargetSchema() map[string]*schema.Schema { Default: false, Type: schema.TypeBool, }, + + // Read-only Values + "agent_version": { + Description: "Current Helm chart version of the agent.", + Computed: true, + Type: schema.TypeString, + }, + "agent_tentacle_version": { + Description: "Current Tentacle version of the agent", + Computed: true, + Type: schema.TypeString, + }, + "agent_upgrade_status": { + Description: "Current upgrade availability status of the agent. One of 'NoUpgrades', 'UpgradeAvailable', 'UpgradeSuggested', 'UpgradeRequired'", + Computed: true, + Type: schema.TypeString, + }, + "agent_helm_release_name": { + Description: "Name of the Helm release that the agent belongs to.", + Computed: true, + Type: schema.TypeString, + }, + "agent_kubernetes_namespace": { + Description: "Name of the Kubernetes namespace where the agent is installed.", + Computed: true, + Type: schema.TypeString, + }, } } + +func getKubernetesAgentDeploymentTargetDataSchema() map[string]*schema.Schema { + dataSchema := getKubernetesAgentDeploymentTargetSchema() + setDataSchema(&dataSchema) + + deploymentTargetDataSchema := getDeploymentTargetDataSchema() + deploymentTargetDataSchema["kubernetes_agent_deployment_targets"] = &schema.Schema{ + Computed: true, + Description: "A list of kubernetes agent deployment targets that match the filter(s).", + Elem: &schema.Resource{Schema: dataSchema}, + Optional: true, + Type: schema.TypeList, + } + + delete(deploymentTargetDataSchema, "communication_styles") + delete(deploymentTargetDataSchema, "deployment_targets") + deploymentTargetDataSchema["id"] = getDataSchemaID() + return deploymentTargetDataSchema +}