Skip to content

Commit 825f2dd

Browse files
authored
feat(onboarding): data source tenant external id (#512)
* feat(onboarding): data source tenant external id * adds external id as data source for use by sysdig tenants * chore(onboarding): create secure onboarding client * docs: add sysdig_secure_tenant_external_id * fix: lint errcheck
1 parent a50ee6a commit 825f2dd

8 files changed

+143
-22
lines changed

sysdig/data_source_sysdig_secure_trusted_cloud_identity.go renamed to sysdig/data_source_sysdig_secure_onboarding.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import (
66
"time"
77

88
"github.com/aws/aws-sdk-go/aws/arn"
9+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
)
1314

15+
func getSecureOnboardingClient(c SysdigClients) (v2.OnboardingSecureInterface, error) {
16+
return c.sysdigSecureClientV2()
17+
}
18+
1419
func dataSourceSysdigSecureTrustedCloudIdentity() *schema.Resource {
1520
timeout := 5 * time.Minute
1621

@@ -53,7 +58,7 @@ func dataSourceSysdigSecureTrustedCloudIdentity() *schema.Resource {
5358

5459
// Retrieves the information of a resource form the file and loads it in Terraform
5560
func dataSourceSysdigSecureTrustedCloudIdentityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
56-
client, err := getSecureCloudAccountClient(meta.(SysdigClients))
61+
client, err := getSecureOnboardingClient(meta.(SysdigClients))
5762
if err != nil {
5863
return diag.FromErr(err)
5964
}
@@ -88,3 +93,43 @@ func dataSourceSysdigSecureTrustedCloudIdentityRead(ctx context.Context, d *sche
8893
}
8994
return nil
9095
}
96+
97+
func dataSourceSysdigSecureTenantExternalID() *schema.Resource {
98+
timeout := 5 * time.Minute
99+
100+
return &schema.Resource{
101+
ReadContext: dataSourceSysdigSecureTenantExternalIDRead,
102+
103+
Timeouts: &schema.ResourceTimeout{
104+
Read: schema.DefaultTimeout(timeout),
105+
},
106+
107+
Schema: map[string]*schema.Schema{
108+
"external_id": {
109+
Type: schema.TypeString,
110+
Computed: true,
111+
},
112+
},
113+
}
114+
}
115+
116+
// Retrieves the information of a resource form the file and loads it in Terraform
117+
func dataSourceSysdigSecureTenantExternalIDRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
118+
client, err := getSecureOnboardingClient(meta.(SysdigClients))
119+
if err != nil {
120+
return diag.FromErr(err)
121+
}
122+
123+
externalId, err := client.GetTenantExternalIDSecure(ctx)
124+
if err != nil {
125+
return diag.FromErr(err)
126+
}
127+
128+
d.SetId(externalId)
129+
err = d.Set("external_id", externalId)
130+
if err != nil {
131+
return diag.FromErr(err)
132+
}
133+
134+
return nil
135+
}

sysdig/data_source_sysdig_secure_trusted_cloud_identity_test.go renamed to sysdig/data_source_sysdig_secure_onboarding_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,23 @@ data "sysdig_secure_trusted_cloud_identity" "trusted_identity" {
6161
}
6262
`
6363
}
64+
65+
func TestAccTenantExternalIDDataSource(t *testing.T) {
66+
resource.ParallelTest(t, resource.TestCase{
67+
PreCheck: func() {
68+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
69+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
70+
}
71+
},
72+
ProviderFactories: map[string]func() (*schema.Provider, error){
73+
"sysdig": func() (*schema.Provider, error) {
74+
return sysdig.Provider(), nil
75+
},
76+
},
77+
Steps: []resource.TestStep{
78+
{
79+
Config: `data "sysdig_secure_tenant_external_id" "external_id" {}`,
80+
},
81+
},
82+
})
83+
}

sysdig/internal/client/v2/cloud_account.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const (
1111
cloudAccountsWithExternalIDPath = "%s/api/cloud/v2/accounts?includeExternalID=true&upsert=true"
1212
cloudAccountPath = "%s/api/cloud/v2/accounts/%s"
1313
cloudAccountWithExternalIDPath = "%s/api/cloud/v2/accounts/%s?includeExternalID=true"
14-
trustedCloudIdentityPath = "%s/api/cloud/v2/%s/trustedIdentity"
1514
providersPath = "%v/api/v2/providers"
1615
)
1716

@@ -21,7 +20,6 @@ type CloudAccountSecureInterface interface {
2120
GetCloudAccountSecure(ctx context.Context, accountID string) (*CloudAccountSecure, error)
2221
DeleteCloudAccountSecure(ctx context.Context, accountID string) error
2322
UpdateCloudAccountSecure(ctx context.Context, accountID string, cloudAccount *CloudAccountSecure) (*CloudAccountSecure, error)
24-
GetTrustedCloudIdentitySecure(ctx context.Context, provider string) (string, error)
2523
}
2624

2725
type CloudAccountMonitorInterface interface {
@@ -99,20 +97,6 @@ func (client *Client) UpdateCloudAccountSecure(ctx context.Context, accountID st
9997
return Unmarshal[*CloudAccountSecure](response.Body)
10098
}
10199

102-
func (client *Client) GetTrustedCloudIdentitySecure(ctx context.Context, provider string) (string, error) {
103-
response, err := client.requester.Request(ctx, http.MethodGet, client.trustedCloudIdentityURL(provider), nil)
104-
if err != nil {
105-
return "", err
106-
}
107-
defer response.Body.Close()
108-
109-
if response.StatusCode != http.StatusOK {
110-
return "", client.ErrorFromResponse(response)
111-
}
112-
113-
return Unmarshal[string](response.Body)
114-
}
115-
116100
func (client *Client) cloudAccountsURL(includeExternalID bool) string {
117101
if includeExternalID {
118102
return fmt.Sprintf(cloudAccountsWithExternalIDPath, client.config.url)
@@ -127,10 +111,6 @@ func (client *Client) cloudAccountURL(accountID string, includeExternalID bool)
127111
return fmt.Sprintf(cloudAccountPath, client.config.url, accountID)
128112
}
129113

130-
func (client *Client) trustedCloudIdentityURL(provider string) string {
131-
return fmt.Sprintf(trustedCloudIdentityPath, client.config.url, provider)
132-
}
133-
134114
func (client *Client) CreateCloudAccountMonitor(ctx context.Context, provider *CloudAccountMonitor) (*CloudAccountMonitor, error) {
135115
payload, err := Marshal(provider)
136116
if err != nil {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const (
10+
onboardingTrustedIdentityPath = "%s/api/secure/onboarding/v2/trustedIdentity?provider=%s"
11+
onboardingTenantExternaIDPath = "%s/api/secure/onboarding/v2/externalID"
12+
)
13+
14+
type OnboardingSecureInterface interface {
15+
Base
16+
GetTrustedCloudIdentitySecure(ctx context.Context, provider string) (string, error)
17+
GetTenantExternalIDSecure(ctx context.Context) (string, error)
18+
}
19+
20+
func (client *Client) GetTrustedCloudIdentitySecure(ctx context.Context, provider string) (string, error) {
21+
response, err := client.requester.Request(ctx, http.MethodGet, fmt.Sprintf(onboardingTrustedIdentityPath, client.config.url, provider), nil)
22+
if err != nil {
23+
return "", err
24+
}
25+
defer response.Body.Close()
26+
27+
if response.StatusCode != http.StatusOK {
28+
return "", client.ErrorFromResponse(response)
29+
}
30+
31+
return Unmarshal[string](response.Body)
32+
}
33+
34+
func (client *Client) GetTenantExternalIDSecure(ctx context.Context) (string, error) {
35+
response, err := client.requester.Request(ctx, http.MethodGet, fmt.Sprintf(onboardingTenantExternaIDPath, client.config.url), nil)
36+
if err != nil {
37+
return "", err
38+
}
39+
defer response.Body.Close()
40+
41+
if response.StatusCode != http.StatusOK {
42+
return "", client.ErrorFromResponse(response)
43+
}
44+
45+
return Unmarshal[string](response.Body)
46+
}

sysdig/internal/client/v2/sysdig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type SysdigSecure interface {
4848
OrganizationSecureInterface
4949
CloudauthAccountComponentSecureInterface
5050
CloudauthAccountFeatureSecureInterface
51+
OnboardingSecureInterface
5152
}
5253

5354
func (sr *SysdigRequest) Request(ctx context.Context, method string, url string, payload io.Reader) (*http.Response, error) {

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func (p *SysdigProvider) Provider() *schema.Provider {
195195
},
196196
DataSourcesMap: map[string]*schema.Resource{
197197
"sysdig_secure_trusted_cloud_identity": dataSourceSysdigSecureTrustedCloudIdentity(),
198+
"sysdig_secure_tenant_external_id": dataSourceSysdigSecureTenantExternalID(),
198199
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
199200
"sysdig_secure_notification_channel_pagerduty": dataSourceSysdigSecureNotificationChannelPagerduty(),
200201
"sysdig_secure_notification_channel_email": dataSourceSysdigSecureNotificationChannelEmail(),

sysdig/resource_sysdig_monitor_alert_downtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func downtimeAlertToResourceData(alert *v2.Alert, data *schema.ResourceData) (er
158158
}
159159

160160
var trigger_after_pct float64
161-
fmt.Sscanf(alert.Condition, "avg(timeAvg(uptime)) <= %f", &trigger_after_pct)
161+
_, _ = fmt.Sscanf(alert.Condition, "avg(timeAvg(uptime)) <= %f", &trigger_after_pct)
162162
trigger_after_pct = (1 - trigger_after_pct) * 100
163163

164164
_ = data.Set("trigger_after_pct", int(trigger_after_pct))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
subcategory: "Sysdig Secure"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_secure_tenant_external_id"
5+
description: |-
6+
Retrieves information about the Sysdig Secure Tenant External ID
7+
---
8+
9+
# Data Source: sysdig_secure_tenant_external_id
10+
11+
Retrieves information about the Sysdig Secure Tenant External ID
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+
```terraform
18+
data "sysdig_secure_tenant_external_id" "external_id" {}
19+
```
20+
21+
## Argument Reference
22+
23+
## Attributes Reference
24+
25+
In addition to all arguments above, the following attributes are exported:
26+
27+
* `external_id` - String identifier for external id value
28+

0 commit comments

Comments
 (0)