Skip to content

Commit

Permalink
feat(teams): add ibm platform metrics in teams (#290)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
filiptubic authored Apr 3, 2023
1 parent aa9fec5 commit 50fd79a
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ jobs:
env:
SYSDIG_IBM_MONITOR_API_KEY: ${{ secrets.TERRAFORM_IBM_API_KEY }}
SYSDIG_IBM_MONITOR_INSTANCE_ID: ${{ secrets.TERRAFORM_IBM_MONITOR_INSTANCE_ID }}
SYSDIG_IBM_MONITOR_URL: "https://us-south.monitoring.test.cloud.ibm.com"
SYSDIG_IBM_MONITOR_IAM_URL: "https://iam.test.cloud.ibm.com"
SYSDIG_MONITOR_URL: "https://us-south.monitoring.test.cloud.ibm.com"
1 change: 0 additions & 1 deletion sysdig/internal/client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

const (
SysdigTeamIDHeader = "SysdigTeamID"
AuthorizationHeader = "Authorization"
ContentTypeHeader = "Content-Type"
ContentTypeJSON = "application/json"
Expand Down
7 changes: 0 additions & 7 deletions sysdig/internal/client/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ type config struct {
token string
insecure bool
extraHeaders map[string]string
teamID string
ibmInstanceID string
ibmAPIKey string
ibmIamURL string
Expand Down Expand Up @@ -37,12 +36,6 @@ func WithExtraHeaders(headers map[string]string) ClientOption {
}
}

func WithTeamID(teamID string) ClientOption {
return func(c *config) {
c.teamID = teamID
}
}

func WithIBMInstanceID(instanceID string) ClientOption {
return func(c *config) {
c.ibmInstanceID = instanceID
Expand Down
3 changes: 0 additions & 3 deletions sysdig/internal/client/v2/ibm.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ func (ir *IBMRequest) Request(ctx context.Context, method string, url string, pa
}

r = r.WithContext(ctx)
if ir.config.teamID != "" {
r.Header.Set(SysdigTeamIDHeader, ir.config.teamID)
}
r.Header.Set(IBMInstanceIDHeader, ir.config.ibmInstanceID)
r.Header.Set(AuthorizationHeader, fmt.Sprintf("Bearer %s", token))
r.Header.Set(ContentTypeHeader, ContentTypeJSON)
Expand Down
5 changes: 0 additions & 5 deletions sysdig/internal/client/v2/ibm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestIBMClient_DoIBMRequest(t *testing.T) {
instanceID := "instance ID"
apiKey := "api key"
token := "token"
teamID := "team ID"
iamEndpointCalled := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == IBMIAMPath {
Expand All @@ -59,17 +58,13 @@ func TestIBMClient_DoIBMRequest(t *testing.T) {
if value := r.Header.Get(IBMInstanceIDHeader); value != instanceID {
t.Errorf("expected instance id %v, got %v", instanceID, value)
}
if value := r.Header.Get(SysdigTeamIDHeader); value != teamID {
t.Errorf("expected team id %v, got %v", teamID, value)
}
}))

c := newIBMClient(
WithIBMInstanceID(instanceID),
WithIBMAPIKey(apiKey),
WithIBMIamURL(server.URL),
WithURL(server.URL),
WithTeamID(teamID),
)

url := fmt.Sprintf("%s/foo/bar", server.URL)
Expand Down
47 changes: 26 additions & 21 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package v2

type Team struct {
UserRoles []UserRoles `json:"userRoles,omitempty"`
Description string `json:"description"`
Name string `json:"name"`
ID int `json:"id,omitempty"`
Version int `json:"version,omitempty"`
Origin string `json:"origin,omitempty"`
LastUpdated int64 `json:"lastUpdated,omitempty"`
EntryPoint *EntryPoint `json:"entryPoint,omitempty"`
Theme string `json:"theme"`
CustomerID int `json:"customerId,omitempty"`
DateCreated int64 `json:"dateCreated,omitempty"`
Products []string `json:"products,omitempty"`
Show string `json:"show,omitempty"`
Immutable bool `json:"immutable,omitempty"`
CanUseSysdigCapture *bool `json:"canUseSysdigCapture,omitempty"`
CanUseCustomEvents *bool `json:"canUseCustomEvents,omitempty"`
CanUseAwsMetrics *bool `json:"canUseAwsMetrics,omitempty"`
CanUseBeaconMetrics *bool `json:"canUseBeaconMetrics,omitempty"`
UserCount int `json:"userCount,omitempty"`
Filter string `json:"filter,omitempty"`
DefaultTeam bool `json:"default,omitempty"`
UserRoles []UserRoles `json:"userRoles,omitempty"`
Description string `json:"description"`
Name string `json:"name"`
ID int `json:"id,omitempty"`
Version int `json:"version,omitempty"`
Origin string `json:"origin,omitempty"`
LastUpdated int64 `json:"lastUpdated,omitempty"`
EntryPoint *EntryPoint `json:"entryPoint,omitempty"`
Theme string `json:"theme"`
CustomerID int `json:"customerId,omitempty"`
DateCreated int64 `json:"dateCreated,omitempty"`
Products []string `json:"products,omitempty"`
Show string `json:"show,omitempty"`
Immutable bool `json:"immutable,omitempty"`
CanUseSysdigCapture *bool `json:"canUseSysdigCapture,omitempty"`
CanUseCustomEvents *bool `json:"canUseCustomEvents,omitempty"`
CanUseAwsMetrics *bool `json:"canUseAwsMetrics,omitempty"`
CanUseBeaconMetrics *bool `json:"canUseBeaconMetrics,omitempty"`
UserCount int `json:"userCount,omitempty"`
Filter string `json:"filter,omitempty"`
NamespaceFilters *NamespaceFilters `json:"namespaceFilters,omitempty"`
DefaultTeam bool `json:"default,omitempty"`
}

type NamespaceFilters struct {
IBMPlatformMetrics *string `json:"ibmPlatformMetrics"`
}

type UserRoles struct {
Expand Down
22 changes: 0 additions & 22 deletions sysdig/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func Provider() *schema.Provider {
undocumentedCodeMsg := "You are using undocumented provider argument which can change in future releases. Please do not use it."
return &schema.Provider{
Schema: map[string]*schema.Schema{
"sysdig_secure_api_token": {
Expand Down Expand Up @@ -48,41 +47,20 @@ func Provider() *schema.Provider {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ibm_monitor_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_URL", nil),
Deprecated: undocumentedCodeMsg,
},
"ibm_monitor_iam_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_IAM_URL", nil),
Deprecated: undocumentedCodeMsg,
},
"ibm_monitor_instance_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_INSTANCE_ID", nil),
Deprecated: undocumentedCodeMsg,
},
"ibm_monitor_api_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_API_KEY", nil),
Deprecated: undocumentedCodeMsg,
},
"ibm_monitor_insecure_tls": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_INSECURE_TLS", nil),
Deprecated: undocumentedCodeMsg,
},
"ibm_monitor_team_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_IBM_MONITOR_TEAM_ID", nil),
Deprecated: undocumentedCodeMsg,
},
},
ResourcesMap: map[string]*schema.Resource{
Expand Down
67 changes: 60 additions & 7 deletions sysdig/resource_sysdig_monitor_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func resourceSysdigMonitorTeam() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"enable_ibm_platform_metrics": {
Type: schema.TypeBool,
Optional: true,
},
"ibm_platform_metrics": {
Type: schema.TypeString,
Optional: true,
},
"can_use_sysdig_capture": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -136,12 +144,13 @@ func getMonitorTeamClient(c SysdigClients) (v2.TeamInterface, error) {
}

func resourceSysdigMonitorTeamCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := getMonitorTeamClient(meta.(SysdigClients))
clients := meta.(SysdigClients)
client, err := getMonitorTeamClient(clients)
if err != nil {
return diag.FromErr(err)
}

team := teamFromResourceData(d)
team := teamFromResourceData(d, clients.GetClientType())
team.Products = []string{"SDC"}

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

d.SetId(strconv.Itoa(team.ID))
_ = d.Set("version", team.Version)
resourceSysdigMonitorTeamRead(ctx, d, meta)

return nil
}

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

if clients.GetClientType() == IBMMonitor {
resourceSysdigMonitorTeamReadIBM(d, &t)
}

return nil
}

func resourceSysdigMonitorTeamReadIBM(d *schema.ResourceData, t *v2.Team) {
var ibmPlatformMetrics *string
if t.NamespaceFilters != nil {
ibmPlatformMetrics = t.NamespaceFilters.IBMPlatformMetrics
}
_ = d.Set("enable_ibm_platform_metrics", t.CanUseBeaconMetrics)
_ = d.Set("ibm_platform_metrics", ibmPlatformMetrics)
}

func userMonitorRolesToSet(userRoles []v2.UserRoles) (res []map[string]interface{}) {
for _, role := range userRoles {
if role.Admin { // Admins are added by default, so skip them
Expand Down Expand Up @@ -214,12 +237,13 @@ func entrypointToSet(entrypoint *v2.EntryPoint) (res []map[string]interface{}) {
}

func resourceSysdigMonitorTeamUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := getMonitorTeamClient(meta.(SysdigClients))
clients := meta.(SysdigClients)
client, err := getMonitorTeamClient(clients)
if err != nil {
return diag.FromErr(err)
}

t := teamFromResourceData(d)
t := teamFromResourceData(d, clients.GetClientType())
t.Products = []string{"SDC"}

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

resourceSysdigMonitorTeamRead(ctx, d, meta)
return nil
}

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

func teamFromResourceData(d *schema.ResourceData) v2.Team {
func updateNamespaceFilters(filters *v2.NamespaceFilters, update v2.NamespaceFilters) *v2.NamespaceFilters {
if filters == nil {
filters = &v2.NamespaceFilters{}
}

if update.IBMPlatformMetrics != nil {
filters.IBMPlatformMetrics = update.IBMPlatformMetrics
}

return filters
}

func teamFromResourceData(d *schema.ResourceData, clientType ClientType) v2.Team {
canUseSysdigCapture := d.Get("can_use_sysdig_capture").(bool)
canUseCustomEvents := d.Get("can_see_infrastructure_events").(bool)
canUseAwsMetrics := d.Get("can_use_aws_data").(bool)
Expand Down Expand Up @@ -282,5 +319,21 @@ func teamFromResourceData(d *schema.ResourceData) v2.Team {
t.EntryPoint.Selection = val.(string)
}

if clientType == IBMMonitor {
teamFromResourceDataIBM(d, &t)
}

return t
}

func teamFromResourceDataIBM(d *schema.ResourceData, t *v2.Team) {
canUseBeaconMetrics := d.Get("enable_ibm_platform_metrics").(bool)
t.CanUseBeaconMetrics = &canUseBeaconMetrics

if v, ok := d.GetOk("ibm_platform_metrics"); ok {
metrics := v.(string)
t.NamespaceFilters = updateNamespaceFilters(t.NamespaceFilters, v2.NamespaceFilters{
IBMPlatformMetrics: &metrics,
})
}
}
16 changes: 16 additions & 0 deletions sysdig/resource_sysdig_monitor_team_ibm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func TestAccMonitorIBMTeam(t *testing.T) {
{
Config: monitorTeamWithFullConfigIBM(rText()),
},
{
Config: monitorTeamWithPlatformMetricsIBM(rText()),
},
{
ResourceName: "sysdig_monitor_team.sample",
ImportState: true,
Expand All @@ -62,3 +65,16 @@ resource "sysdig_monitor_team" "sample" {
}
}`, name, name)
}

func monitorTeamWithPlatformMetricsIBM(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_team" "sample" {
name = "sample-%s"
enable_ibm_platform_metrics = true
ibm_platform_metrics = "foo in (\"0\") and bar in (\"3\")"
entrypoint {
type = "Dashboards"
}
}`, name)
}
6 changes: 2 additions & 4 deletions sysdig/sysdig_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type ibmVariables struct {
iamURL string
instanceID string
apiKey string
teamID string
}

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

if apiURL, ok = data.GetOk("ibm_monitor_url"); !ok {
if apiURL, ok = data.GetOk("sysdig_monitor_url"); !ok {
return nil, errors.New("missing monitor IBM URL")
}

Expand All @@ -137,13 +136,12 @@ func getIBMMonitorVariables(data *schema.ResourceData) (*ibmVariables, error) {
return &ibmVariables{
globalVariables: &globalVariables{
apiURL: apiURL.(string),
insecure: data.Get("ibm_monitor_insecure_tls").(bool),
insecure: data.Get("sysdig_monitor_insecure_tls").(bool),
extraHeaders: getExtraHeaders(data),
},
iamURL: iamURL.(string),
instanceID: instanceID.(string),
apiKey: apiKey.(string),
teamID: data.Get("ibm_monitor_team_id").(string),
}, nil
}

Expand Down
Loading

0 comments on commit 50fd79a

Please sign in to comment.