Skip to content

Commit d464ef6

Browse files
feat: Add secure-trusted-cloud-account datasource (#113)
* Add secure-trusted-cloud-account datasource * add provider param * use non-keyword * rename endpoints and functions to cloudIdentity * rename missed function * use identity instead of arn to be cloud agnostic, add docs * fix title
1 parent 036d69a commit d464ef6

File tree

8 files changed

+195
-3
lines changed

8 files changed

+195
-3
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceSysdigSecureTrustedCloudIdentity() *schema.Resource {
12+
timeout := 5 * time.Minute
13+
14+
return &schema.Resource{
15+
ReadContext: dataSourceSysdigSecureTrustedCloudIdentityRead,
16+
17+
Timeouts: &schema.ResourceTimeout{
18+
Read: schema.DefaultTimeout(timeout),
19+
},
20+
21+
Schema: map[string]*schema.Schema{
22+
"cloud_provider": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
"identity": {
27+
Type: schema.TypeString,
28+
Computed: true,
29+
},
30+
},
31+
}
32+
}
33+
34+
// Retrieves the information of a resource form the file and loads it in Terraform
35+
func dataSourceSysdigSecureTrustedCloudIdentityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
36+
client, err := meta.(SysdigClients).sysdigSecureClient()
37+
if err != nil {
38+
return diag.FromErr(err)
39+
}
40+
41+
identity, err := client.GetTrustedCloudIdentity(ctx, d.Get("cloud_provider").(string))
42+
if err != nil {
43+
return diag.FromErr(err)
44+
}
45+
46+
d.SetId(identity)
47+
d.Set("identity", identity)
48+
49+
return nil
50+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package sysdig_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
10+
"github.com/draios/terraform-provider-sysdig/sysdig"
11+
)
12+
13+
func TestAccTrustedCloudIdentityDataSource(t *testing.T) {
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() {
16+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
17+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
18+
}
19+
},
20+
ProviderFactories: map[string]func() (*schema.Provider, error){
21+
"sysdig": func() (*schema.Provider, error) {
22+
return sysdig.Provider(), nil
23+
},
24+
},
25+
Steps: []resource.TestStep{
26+
{
27+
Config: trustedIdentityDatasource(),
28+
},
29+
},
30+
})
31+
}
32+
33+
func trustedIdentityDatasource() string {
34+
return `
35+
data "sysdig_secure_trusted_cloud_identity" "trusted_identity" {
36+
cloud_provider = "aws"
37+
}
38+
`
39+
}

sysdig/internal/client/secure/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type SysdigSecureClient interface {
5757
GetCloudAccountById(context.Context, string) (*CloudAccount, error)
5858
DeleteCloudAccount(context.Context, string) error
5959
UpdateCloudAccount(context.Context, string, *CloudAccount) (*CloudAccount, error)
60+
GetTrustedCloudIdentity(context.Context, string) (string, error)
6061
}
6162

6263
func WithExtraHeaders(client SysdigSecureClient, extraHeaders map[string]string) SysdigSecureClient {

sysdig/internal/client/secure/cloud_account.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (client *sysdigSecureClient) cloudAccountByIdURL(accountID string, includeE
2121
return fmt.Sprintf("%s/api/cloud/v2/accounts/%s", client.URL, accountID)
2222
}
2323

24+
func (client *sysdigSecureClient) trustedCloudIdentityURL(provider string) string {
25+
return fmt.Sprintf("%s/api/cloud/v2/%s/trustedIdentity", client.URL, provider)
26+
}
27+
2428
func (client *sysdigSecureClient) CreateCloudAccount(ctx context.Context, cloudAccount *CloudAccount) (*CloudAccount, error) {
2529
response, err := client.doSysdigSecureRequest(ctx, http.MethodPost, client.cloudAccountURL(true), cloudAccount.ToJSON())
2630
if err != nil {
@@ -84,3 +88,22 @@ func (client *sysdigSecureClient) UpdateCloudAccount(ctx context.Context, accoun
8488
bodyBytes, _ := ioutil.ReadAll(response.Body)
8589
return CloudAccountFromJSON(bodyBytes), nil
8690
}
91+
92+
func (client *sysdigSecureClient) GetTrustedCloudIdentity(ctx context.Context, provider string) (string, error) {
93+
response, err := client.doSysdigSecureRequest(ctx, http.MethodGet, client.trustedCloudIdentityURL(provider), nil)
94+
if err != nil {
95+
return "", err
96+
}
97+
defer response.Body.Close()
98+
99+
if response.StatusCode != http.StatusOK {
100+
return "", errorFromResponse(response)
101+
}
102+
103+
bodyBytes, err := ioutil.ReadAll(response.Body)
104+
if err != nil {
105+
return "", err
106+
}
107+
108+
return string(bodyBytes), nil
109+
}

sysdig/provider.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ func Provider() *schema.Provider {
8888
"sysdig_monitor_team": resourceSysdigMonitorTeam(),
8989
},
9090
DataSourcesMap: map[string]*schema.Resource{
91-
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
92-
"sysdig_current_user": dataSourceSysdigCurrentUser(),
93-
"sysdig_user": dataSourceSysdigUser(),
91+
"sysdig_secure_trusted_cloud_identity": dataSourceSysdigSecureTrustedCloudIdentity(),
92+
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
93+
"sysdig_current_user": dataSourceSysdigCurrentUser(),
94+
"sysdig_user": dataSourceSysdigUser(),
9495
},
9596
ConfigureContextFunc: providerConfigure,
9697
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: "sysdig"
3+
page_title: "Sysdig: sysdig_secure_trusted_cloud_identity"
4+
sidebar_current: "docs-sysdig-secure-trusted-cloud-identity-ds"
5+
description: |-
6+
Retrieves information about the Sysdig Secure Trusted Cloud Identity
7+
---
8+
9+
# sysdig\_secure\_trusted_cloud_identity
10+
11+
Retrieves information about the Sysdig Secure Trusted Cloud Identity
12+
13+
`~> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.`
14+
15+
## Example usage
16+
17+
```hcl
18+
data "sysdig_secure_trusted_cloud_identity" "trusted_identity" {
19+
cloud_provider = "aws"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `cloud_provider` - (Required) The cloud provider in which the account exists. Currently supported providers are `aws`, `gcp` and `azure`
26+
27+
28+
## Attributes Reference
29+
30+
* `identity` - Sysdig's identity (User/Role/etc) that should be used to create a trust relationship allowing Sysdig access to your cloud account.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
layout: "sysdig"
3+
page_title: "Sysdig: sysdig_secure_cloud_account"
4+
sidebar_current: "docs-sysdig_secure_cloud_account"
5+
description: |-
6+
Creates a Sysdig Secure Cloud Account.
7+
---
8+
9+
# sysdig\_secure\_cloud_account
10+
11+
Creates a Sysdig Secure Cloud Account.
12+
13+
`~> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.`
14+
15+
## Example usage
16+
17+
```hcl
18+
resource "sysdig_secure_cloud_account" "sample" {
19+
account_id = "123456789012"
20+
cloud_provider = "aws"
21+
alias = "prod"
22+
role_enabled = "false"
23+
}
24+
```
25+
26+
## Argument Reference
27+
28+
* `account_id` - (Required) The unique identifier of the cloud account. e.g. for AWS: `123456789012`,
29+
30+
* `cloud_provider` - (Required) The cloud provider in which the account exists. Currently supported providers are `aws`, `gcp` and `azure`
31+
32+
* `alias` - (Optional) A human friendly alias for `account_id`.
33+
34+
* `role_enabled` - (Optional) Whether or not a role with the name `SysdigCloud` is provisioned withing this account, that Sysdig has permission to AssumeRole in order to run Benchmarks. Default: `false`.
35+
36+
## Import
37+
38+
Secure Teams can be imported using the `account_id`, e.g.
39+
40+
```
41+
$ terraform import sysdig_secure_cloud_account.sample 123456789012
42+
```

website/sysdig.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
<li<%= sidebar_current("docs-sysdig-secure-notification-channel-ds") %>>
1818
<a href="/docs/providers/sysdig/d/sysdig_secure_notification_channel.html">sysdig_secure_notification_channel</a>
1919
</li>
20+
<li<%= sidebar_current("docs-sysdig-secure-trusted-cloud-identity-ds") %>>
21+
<a href="/docs/providers/sysdig/d/sysdig_secure_trusted_cloud_identity.html">sysdig_secure_trusted_cloud_identity</a>
22+
</li>
2023
</ul>
2124
</li>
2225
<li<%= sidebar_current("docs-sysdig-secure-resources") %>>
2326
<a href="#">Resources</a>
2427
<ul class="nav nav-auto-expand">
28+
<li<%= sidebar_current("docs-sysdig-secure-cloud-account") %>>
29+
<a href="/docs/providers/sysdig/r/sysdig_secure_cloud_account.html">sysdig_secure_cloud_account</a>
30+
</li>
2531
<li<%= sidebar_current("docs-sysdig-secure-list") %>>
2632
<a href="/docs/providers/sysdig/r/sysdig_secure_list.html">sysdig_secure_list</a>
2733
</li>

0 commit comments

Comments
 (0)