|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/draios/terraform-provider-sysdig/sysdig/secure" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func resourceSysdigSecureTeam() *schema.Resource { |
| 11 | + timeout := 30 * time.Second |
| 12 | + |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceSysdigTeamCreate, |
| 15 | + Update: resourceSysdigTeamUpdate, |
| 16 | + Read: resourceSysdigTeamRead, |
| 17 | + Delete: resourceSysdigTeamDelete, |
| 18 | + |
| 19 | + Timeouts: &schema.ResourceTimeout{ |
| 20 | + Create: schema.DefaultTimeout(timeout), |
| 21 | + }, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "theme": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Default: "#73A1F7", |
| 28 | + }, |
| 29 | + "name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + "description": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + }, |
| 37 | + "scope_by": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Default: "container", |
| 41 | + }, |
| 42 | + "filter": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + }, |
| 46 | + "use_sysdig_capture": { |
| 47 | + Type: schema.TypeBool, |
| 48 | + Optional: true, |
| 49 | + Default: true, |
| 50 | + }, |
| 51 | + "user_roles": { |
| 52 | + Type: schema.TypeSet, |
| 53 | + Optional: true, |
| 54 | + Elem: &schema.Resource{ |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "email": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + }, |
| 60 | + |
| 61 | + "role": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Optional: true, |
| 64 | + Default: "ROLE_TEAM_STANDARD", |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + "default_team": { |
| 70 | + Type: schema.TypeBool, |
| 71 | + Optional: true, |
| 72 | + Default: false, |
| 73 | + }, |
| 74 | + "version": { |
| 75 | + Type: schema.TypeInt, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func resourceSysdigTeamCreate(d *schema.ResourceData, meta interface{}) error { |
| 83 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 84 | + |
| 85 | + team := teamFromResourceData(d) |
| 86 | + |
| 87 | + team, err := client.CreateTeam(team) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + d.SetId(strconv.Itoa(team.ID)) |
| 93 | + d.Set("version", team.Version) |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// Retrieves the information of a resource form the file and loads it in Terraform |
| 99 | +func resourceSysdigTeamRead(d *schema.ResourceData, meta interface{}) error { |
| 100 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 101 | + |
| 102 | + id, _ := strconv.Atoi(d.Id()) |
| 103 | + t, err := client.GetTeamById(id) |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + d.SetId("") |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + d.Set("version", t.Version) |
| 111 | + d.Set("theme", t.Theme) |
| 112 | + d.Set("name", t.Name) |
| 113 | + d.Set("description", t.Description) |
| 114 | + d.Set("scope_by", t.ScopeBy) |
| 115 | + d.Set("filter", t.Filter) |
| 116 | + d.Set("canUseSysdigCapture", t.CanUseSysdigCapture) |
| 117 | + d.Set("default_team", t.DefaultTeam) |
| 118 | + d.Set("user_roles", t.UserRoles) |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceSysdigTeamUpdate(d *schema.ResourceData, meta interface{}) error { |
| 124 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 125 | + |
| 126 | + t := teamFromResourceData(d) |
| 127 | + |
| 128 | + t.Version = d.Get("version").(int) |
| 129 | + t.ID, _ = strconv.Atoi(d.Id()) |
| 130 | + |
| 131 | + _, err := client.UpdateTeam(t) |
| 132 | + |
| 133 | + return err |
| 134 | +} |
| 135 | + |
| 136 | +func resourceSysdigTeamDelete(d *schema.ResourceData, meta interface{}) error { |
| 137 | + client := meta.(*SysdigClients).sysdigSecureClient |
| 138 | + |
| 139 | + id, _ := strconv.Atoi(d.Id()) |
| 140 | + |
| 141 | + return client.DeleteTeam(id) |
| 142 | +} |
| 143 | + |
| 144 | +func teamFromResourceData(d *schema.ResourceData) secure.Team { |
| 145 | + t := secure.Team{ |
| 146 | + Theme: d.Get("theme").(string), |
| 147 | + Name: d.Get("name").(string), |
| 148 | + Description: d.Get("description").(string), |
| 149 | + ScopeBy: d.Get("scope_by").(string), |
| 150 | + Filter: d.Get("filter").(string), |
| 151 | + CanUseSysdigCapture: d.Get("use_sysdig_capture").(bool), |
| 152 | + DefaultTeam: d.Get("default_team").(bool), |
| 153 | + Products: []string{"SDS"}, |
| 154 | + } |
| 155 | + |
| 156 | + userRoles := []secure.UserRoles{} |
| 157 | + for _, userRole := range d.Get("user_roles").(*schema.Set).List() { |
| 158 | + ur := userRole.(map[string]interface{}) |
| 159 | + userRoles = append(userRoles, secure.UserRoles{ |
| 160 | + Email: ur["email"].(string), |
| 161 | + Role: ur["role"].(string), |
| 162 | + }) |
| 163 | + } |
| 164 | + t.UserRoles = userRoles |
| 165 | + |
| 166 | + return t |
| 167 | +} |
0 commit comments