Skip to content

Commit 50fd79a

Browse files
authored
feat(teams): add ibm platform metrics in teams (#290)
* feat(teams): add ibm platform metrics in teams * feat(teams): remove ibm_monitor_url, ibm_monitor_insecure_tls, ibm_monitor_team_id and deprecation message * feat(teams): remove SysdigTeamID header * feat(teams): add note in docs about supported resources on cloud monitoring
1 parent aa9fec5 commit 50fd79a

File tree

12 files changed

+156
-75
lines changed

12 files changed

+156
-75
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ jobs:
6464
env:
6565
SYSDIG_IBM_MONITOR_API_KEY: ${{ secrets.TERRAFORM_IBM_API_KEY }}
6666
SYSDIG_IBM_MONITOR_INSTANCE_ID: ${{ secrets.TERRAFORM_IBM_MONITOR_INSTANCE_ID }}
67-
SYSDIG_IBM_MONITOR_URL: "https://us-south.monitoring.test.cloud.ibm.com"
6867
SYSDIG_IBM_MONITOR_IAM_URL: "https://iam.test.cloud.ibm.com"
68+
SYSDIG_MONITOR_URL: "https://us-south.monitoring.test.cloud.ibm.com"

sysdig/internal/client/v2/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
)
1818

1919
const (
20-
SysdigTeamIDHeader = "SysdigTeamID"
2120
AuthorizationHeader = "Authorization"
2221
ContentTypeHeader = "Content-Type"
2322
ContentTypeJSON = "application/json"

sysdig/internal/client/v2/config.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ type config struct {
55
token string
66
insecure bool
77
extraHeaders map[string]string
8-
teamID string
98
ibmInstanceID string
109
ibmAPIKey string
1110
ibmIamURL string
@@ -37,12 +36,6 @@ func WithExtraHeaders(headers map[string]string) ClientOption {
3736
}
3837
}
3938

40-
func WithTeamID(teamID string) ClientOption {
41-
return func(c *config) {
42-
c.teamID = teamID
43-
}
44-
}
45-
4639
func WithIBMInstanceID(instanceID string) ClientOption {
4740
return func(c *config) {
4841
c.ibmInstanceID = instanceID

sysdig/internal/client/v2/ibm.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ func (ir *IBMRequest) Request(ctx context.Context, method string, url string, pa
8787
}
8888

8989
r = r.WithContext(ctx)
90-
if ir.config.teamID != "" {
91-
r.Header.Set(SysdigTeamIDHeader, ir.config.teamID)
92-
}
9390
r.Header.Set(IBMInstanceIDHeader, ir.config.ibmInstanceID)
9491
r.Header.Set(AuthorizationHeader, fmt.Sprintf("Bearer %s", token))
9592
r.Header.Set(ContentTypeHeader, ContentTypeJSON)

sysdig/internal/client/v2/ibm_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func TestIBMClient_DoIBMRequest(t *testing.T) {
3333
instanceID := "instance ID"
3434
apiKey := "api key"
3535
token := "token"
36-
teamID := "team ID"
3736
iamEndpointCalled := 0
3837
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3938
if r.URL.Path == IBMIAMPath {
@@ -59,17 +58,13 @@ func TestIBMClient_DoIBMRequest(t *testing.T) {
5958
if value := r.Header.Get(IBMInstanceIDHeader); value != instanceID {
6059
t.Errorf("expected instance id %v, got %v", instanceID, value)
6160
}
62-
if value := r.Header.Get(SysdigTeamIDHeader); value != teamID {
63-
t.Errorf("expected team id %v, got %v", teamID, value)
64-
}
6561
}))
6662

6763
c := newIBMClient(
6864
WithIBMInstanceID(instanceID),
6965
WithIBMAPIKey(apiKey),
7066
WithIBMIamURL(server.URL),
7167
WithURL(server.URL),
72-
WithTeamID(teamID),
7368
)
7469

7570
url := fmt.Sprintf("%s/foo/bar", server.URL)

sysdig/internal/client/v2/model.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
package v2
22

33
type Team struct {
4-
UserRoles []UserRoles `json:"userRoles,omitempty"`
5-
Description string `json:"description"`
6-
Name string `json:"name"`
7-
ID int `json:"id,omitempty"`
8-
Version int `json:"version,omitempty"`
9-
Origin string `json:"origin,omitempty"`
10-
LastUpdated int64 `json:"lastUpdated,omitempty"`
11-
EntryPoint *EntryPoint `json:"entryPoint,omitempty"`
12-
Theme string `json:"theme"`
13-
CustomerID int `json:"customerId,omitempty"`
14-
DateCreated int64 `json:"dateCreated,omitempty"`
15-
Products []string `json:"products,omitempty"`
16-
Show string `json:"show,omitempty"`
17-
Immutable bool `json:"immutable,omitempty"`
18-
CanUseSysdigCapture *bool `json:"canUseSysdigCapture,omitempty"`
19-
CanUseCustomEvents *bool `json:"canUseCustomEvents,omitempty"`
20-
CanUseAwsMetrics *bool `json:"canUseAwsMetrics,omitempty"`
21-
CanUseBeaconMetrics *bool `json:"canUseBeaconMetrics,omitempty"`
22-
UserCount int `json:"userCount,omitempty"`
23-
Filter string `json:"filter,omitempty"`
24-
DefaultTeam bool `json:"default,omitempty"`
4+
UserRoles []UserRoles `json:"userRoles,omitempty"`
5+
Description string `json:"description"`
6+
Name string `json:"name"`
7+
ID int `json:"id,omitempty"`
8+
Version int `json:"version,omitempty"`
9+
Origin string `json:"origin,omitempty"`
10+
LastUpdated int64 `json:"lastUpdated,omitempty"`
11+
EntryPoint *EntryPoint `json:"entryPoint,omitempty"`
12+
Theme string `json:"theme"`
13+
CustomerID int `json:"customerId,omitempty"`
14+
DateCreated int64 `json:"dateCreated,omitempty"`
15+
Products []string `json:"products,omitempty"`
16+
Show string `json:"show,omitempty"`
17+
Immutable bool `json:"immutable,omitempty"`
18+
CanUseSysdigCapture *bool `json:"canUseSysdigCapture,omitempty"`
19+
CanUseCustomEvents *bool `json:"canUseCustomEvents,omitempty"`
20+
CanUseAwsMetrics *bool `json:"canUseAwsMetrics,omitempty"`
21+
CanUseBeaconMetrics *bool `json:"canUseBeaconMetrics,omitempty"`
22+
UserCount int `json:"userCount,omitempty"`
23+
Filter string `json:"filter,omitempty"`
24+
NamespaceFilters *NamespaceFilters `json:"namespaceFilters,omitempty"`
25+
DefaultTeam bool `json:"default,omitempty"`
26+
}
27+
28+
type NamespaceFilters struct {
29+
IBMPlatformMetrics *string `json:"ibmPlatformMetrics"`
2530
}
2631

2732
type UserRoles struct {

sysdig/provider.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
)
99

1010
func Provider() *schema.Provider {
11-
undocumentedCodeMsg := "You are using undocumented provider argument which can change in future releases. Please do not use it."
1211
return &schema.Provider{
1312
Schema: map[string]*schema.Schema{
1413
"sysdig_secure_api_token": {
@@ -48,41 +47,20 @@ func Provider() *schema.Provider {
4847
Optional: true,
4948
Elem: &schema.Schema{Type: schema.TypeString},
5049
},
51-
"ibm_monitor_url": {
52-
Type: schema.TypeString,
53-
Optional: true,
54-
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_URL", nil),
55-
Deprecated: undocumentedCodeMsg,
56-
},
5750
"ibm_monitor_iam_url": {
5851
Type: schema.TypeString,
5952
Optional: true,
6053
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_IAM_URL", nil),
61-
Deprecated: undocumentedCodeMsg,
6254
},
6355
"ibm_monitor_instance_id": {
6456
Type: schema.TypeString,
6557
Optional: true,
6658
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_INSTANCE_ID", nil),
67-
Deprecated: undocumentedCodeMsg,
6859
},
6960
"ibm_monitor_api_key": {
7061
Type: schema.TypeString,
7162
Optional: true,
7263
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_API_KEY", nil),
73-
Deprecated: undocumentedCodeMsg,
74-
},
75-
"ibm_monitor_insecure_tls": {
76-
Type: schema.TypeBool,
77-
Optional: true,
78-
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_INSECURE_TLS", nil),
79-
Deprecated: undocumentedCodeMsg,
80-
},
81-
"ibm_monitor_team_id": {
82-
Type: schema.TypeString,
83-
Optional: true,
84-
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_TEAM_ID", nil),
85-
Deprecated: undocumentedCodeMsg,
8664
},
8765
},
8866
ResourcesMap: map[string]*schema.Resource{

sysdig/resource_sysdig_monitor_team.go

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ func resourceSysdigMonitorTeam() *schema.Resource {
5353
Type: schema.TypeString,
5454
Optional: true,
5555
},
56+
"enable_ibm_platform_metrics": {
57+
Type: schema.TypeBool,
58+
Optional: true,
59+
},
60+
"ibm_platform_metrics": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
},
5664
"can_use_sysdig_capture": {
5765
Type: schema.TypeBool,
5866
Optional: true,
@@ -136,12 +144,13 @@ func getMonitorTeamClient(c SysdigClients) (v2.TeamInterface, error) {
136144
}
137145

138146
func resourceSysdigMonitorTeamCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
139-
client, err := getMonitorTeamClient(meta.(SysdigClients))
147+
clients := meta.(SysdigClients)
148+
client, err := getMonitorTeamClient(clients)
140149
if err != nil {
141150
return diag.FromErr(err)
142151
}
143152

144-
team := teamFromResourceData(d)
153+
team := teamFromResourceData(d, clients.GetClientType())
145154
team.Products = []string{"SDC"}
146155

147156
team, err = client.CreateTeam(ctx, team)
@@ -150,14 +159,15 @@ func resourceSysdigMonitorTeamCreate(ctx context.Context, d *schema.ResourceData
150159
}
151160

152161
d.SetId(strconv.Itoa(team.ID))
153-
_ = d.Set("version", team.Version)
162+
resourceSysdigMonitorTeamRead(ctx, d, meta)
154163

155164
return nil
156165
}
157166

158167
// Retrieves the information of a resource form the file and loads it in Terraform
159168
func resourceSysdigMonitorTeamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
160-
client, err := getMonitorTeamClient(meta.(SysdigClients))
169+
clients := meta.(SysdigClients)
170+
client, err := getMonitorTeamClient(clients)
161171
if err != nil {
162172
return diag.FromErr(err)
163173
}
@@ -183,9 +193,22 @@ func resourceSysdigMonitorTeamRead(ctx context.Context, d *schema.ResourceData,
183193
_ = d.Set("user_roles", userMonitorRolesToSet(t.UserRoles))
184194
_ = d.Set("entrypoint", entrypointToSet(t.EntryPoint))
185195

196+
if clients.GetClientType() == IBMMonitor {
197+
resourceSysdigMonitorTeamReadIBM(d, &t)
198+
}
199+
186200
return nil
187201
}
188202

203+
func resourceSysdigMonitorTeamReadIBM(d *schema.ResourceData, t *v2.Team) {
204+
var ibmPlatformMetrics *string
205+
if t.NamespaceFilters != nil {
206+
ibmPlatformMetrics = t.NamespaceFilters.IBMPlatformMetrics
207+
}
208+
_ = d.Set("enable_ibm_platform_metrics", t.CanUseBeaconMetrics)
209+
_ = d.Set("ibm_platform_metrics", ibmPlatformMetrics)
210+
}
211+
189212
func userMonitorRolesToSet(userRoles []v2.UserRoles) (res []map[string]interface{}) {
190213
for _, role := range userRoles {
191214
if role.Admin { // Admins are added by default, so skip them
@@ -214,12 +237,13 @@ func entrypointToSet(entrypoint *v2.EntryPoint) (res []map[string]interface{}) {
214237
}
215238

216239
func resourceSysdigMonitorTeamUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
217-
client, err := getMonitorTeamClient(meta.(SysdigClients))
240+
clients := meta.(SysdigClients)
241+
client, err := getMonitorTeamClient(clients)
218242
if err != nil {
219243
return diag.FromErr(err)
220244
}
221245

222-
t := teamFromResourceData(d)
246+
t := teamFromResourceData(d, clients.GetClientType())
223247
t.Products = []string{"SDC"}
224248

225249
t.Version = d.Get("version").(int)
@@ -230,6 +254,7 @@ func resourceSysdigMonitorTeamUpdate(ctx context.Context, d *schema.ResourceData
230254
return diag.FromErr(err)
231255
}
232256

257+
resourceSysdigMonitorTeamRead(ctx, d, meta)
233258
return nil
234259
}
235260

@@ -248,7 +273,19 @@ func resourceSysdigMonitorTeamDelete(ctx context.Context, d *schema.ResourceData
248273
return nil
249274
}
250275

251-
func teamFromResourceData(d *schema.ResourceData) v2.Team {
276+
func updateNamespaceFilters(filters *v2.NamespaceFilters, update v2.NamespaceFilters) *v2.NamespaceFilters {
277+
if filters == nil {
278+
filters = &v2.NamespaceFilters{}
279+
}
280+
281+
if update.IBMPlatformMetrics != nil {
282+
filters.IBMPlatformMetrics = update.IBMPlatformMetrics
283+
}
284+
285+
return filters
286+
}
287+
288+
func teamFromResourceData(d *schema.ResourceData, clientType ClientType) v2.Team {
252289
canUseSysdigCapture := d.Get("can_use_sysdig_capture").(bool)
253290
canUseCustomEvents := d.Get("can_see_infrastructure_events").(bool)
254291
canUseAwsMetrics := d.Get("can_use_aws_data").(bool)
@@ -282,5 +319,21 @@ func teamFromResourceData(d *schema.ResourceData) v2.Team {
282319
t.EntryPoint.Selection = val.(string)
283320
}
284321

322+
if clientType == IBMMonitor {
323+
teamFromResourceDataIBM(d, &t)
324+
}
325+
285326
return t
286327
}
328+
329+
func teamFromResourceDataIBM(d *schema.ResourceData, t *v2.Team) {
330+
canUseBeaconMetrics := d.Get("enable_ibm_platform_metrics").(bool)
331+
t.CanUseBeaconMetrics = &canUseBeaconMetrics
332+
333+
if v, ok := d.GetOk("ibm_platform_metrics"); ok {
334+
metrics := v.(string)
335+
t.NamespaceFilters = updateNamespaceFilters(t.NamespaceFilters, v2.NamespaceFilters{
336+
IBMPlatformMetrics: &metrics,
337+
})
338+
}
339+
}

sysdig/resource_sysdig_monitor_team_ibm_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func TestAccMonitorIBMTeam(t *testing.T) {
3838
{
3939
Config: monitorTeamWithFullConfigIBM(rText()),
4040
},
41+
{
42+
Config: monitorTeamWithPlatformMetricsIBM(rText()),
43+
},
4144
{
4245
ResourceName: "sysdig_monitor_team.sample",
4346
ImportState: true,
@@ -62,3 +65,16 @@ resource "sysdig_monitor_team" "sample" {
6265
}
6366
}`, name, name)
6467
}
68+
69+
func monitorTeamWithPlatformMetricsIBM(name string) string {
70+
return fmt.Sprintf(`
71+
resource "sysdig_monitor_team" "sample" {
72+
name = "sample-%s"
73+
enable_ibm_platform_metrics = true
74+
ibm_platform_metrics = "foo in (\"0\") and bar in (\"3\")"
75+
76+
entrypoint {
77+
type = "Dashboards"
78+
}
79+
}`, name)
80+
}

sysdig/sysdig_clients.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ type ibmVariables struct {
6767
iamURL string
6868
instanceID string
6969
apiKey string
70-
teamID string
7170
}
7271

7372
func getSysdigMonitorVariables(data *schema.ResourceData) (*sysdigVariables, error) {
@@ -118,7 +117,7 @@ func getIBMMonitorVariables(data *schema.ResourceData) (*ibmVariables, error) {
118117
var ok bool
119118
var apiURL, iamURL, instanceID, apiKey interface{}
120119

121-
if apiURL, ok = data.GetOk("ibm_monitor_url"); !ok {
120+
if apiURL, ok = data.GetOk("sysdig_monitor_url"); !ok {
122121
return nil, errors.New("missing monitor IBM URL")
123122
}
124123

@@ -137,13 +136,12 @@ func getIBMMonitorVariables(data *schema.ResourceData) (*ibmVariables, error) {
137136
return &ibmVariables{
138137
globalVariables: &globalVariables{
139138
apiURL: apiURL.(string),
140-
insecure: data.Get("ibm_monitor_insecure_tls").(bool),
139+
insecure: data.Get("sysdig_monitor_insecure_tls").(bool),
141140
extraHeaders: getExtraHeaders(data),
142141
},
143142
iamURL: iamURL.(string),
144143
instanceID: instanceID.(string),
145144
apiKey: apiKey.(string),
146-
teamID: data.Get("ibm_monitor_team_id").(string),
147145
}, nil
148146
}
149147

0 commit comments

Comments
 (0)