Skip to content

Commit

Permalink
feat(data-source): new lacework_agent_access_token
Browse files Browse the repository at this point in the history
To have parity with our resource `lacework_agent_access_token` we are
adding a new data source to retrieve Agent access tokens by looking up
for the Token Alias. (a.k.a Token Name)

Contributes #41

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Dec 8, 2020
1 parent de8b2cc commit 6283e63
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
9 changes: 9 additions & 0 deletions examples/data_source_lacework_agent_access_token/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "lacework" {}

data "lacework_agent_access_token" "k8s" {
name = "k8s-deployments"
}

output "lacework_agent_access_token" {
value = data.lacework_agent_access_token.k8s.token
}
52 changes: 52 additions & 0 deletions lacework/data_source_lacework_agent_access_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package lacework

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/lacework/go-sdk/api"
)

func dataSourceLaceworkAgentAccessToken() *schema.Resource {
return &schema.Resource{
Read: dataSourceLaceworkAgentAccessTokenRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}

func dataSourceLaceworkAgentAccessTokenRead(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

log.Printf("[INFO] Lookup agent access token.")
response, err := lacework.Agents.ListTokens()
if err != nil {
return err
}

lookupName := d.Get("name").(string)
for _, token := range response.Data {
if token.TokenAlias == lookupName {
log.Printf("[INFO] agent access token found. name=%s, description=%s, enabled=%t",
token.TokenAlias, token.Props.Description, token.Status())

d.Set("token", token.AccessToken)
d.SetId(token.TokenAlias)

return nil
}
}

return fmt.Errorf("Agent access token with name '%s' was not found.", lookupName)
}
5 changes: 3 additions & 2 deletions lacework/data_source_lacework_api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ func dataSourceLaceworkApiToken() *schema.Resource {
Read: dataSourceLaceworkApiTokenRead,
Schema: map[string]*schema.Schema{
"token": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion lacework/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"lacework_api_token": dataSourceLaceworkApiToken(),
"lacework_api_token": dataSourceLaceworkApiToken(),
"lacework_agent_access_token": dataSourceLaceworkAgentAccessToken(),
},

ConfigureFunc: providerConfigure,
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/agent_access_token.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "Agents"
layout: "lacework"
page_title: "Lacework: lacework_agent_access_token"
description: |-
Lookup agent access token.
---

# lacework\_agent\_access\_token

Retrieve Lacework agent access tokens.

-> **Note:** To list all agent access tokens in your Lacework account, use the
Lacework CLI command `lacework agent token list`. To install this tool follow
[this documentation](https://github.com/lacework/go-sdk/wiki/CLI-Documentation#installation).

## Example Usage

```hcl
data "lacework_agent_access_token" "k8s" {
name = "k8s-deployments"
}
```

## Argument Reference

* `name` - (Required) The agent access token name.

## Attribute Reference

The following attributes are exported:

* `token` - The agent access token.
10 changes: 10 additions & 0 deletions website/lacework.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@

</ul>
</li>
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">

<li>
<a href="/docs/providers/lacework/d/agent_access_token.html">lacework_agent_access_token</a>
</li>

</ul>
</li>
</ul>
</li>

Expand Down

0 comments on commit 6283e63

Please sign in to comment.