|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceSysdigGroupMappingConfig() *schema.Resource { |
| 13 | + timeout := 5 * time.Minute |
| 14 | + return &schema.Resource{ |
| 15 | + ReadContext: resourceSysdigGroupMappingConfigRead, |
| 16 | + CreateContext: resourceSysdigGroupMappingConfigCreate, |
| 17 | + UpdateContext: resourceSysdigGroupMappingConfigUpdate, |
| 18 | + DeleteContext: resourceSysdigGroupMappingConfigDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + Timeouts: &schema.ResourceTimeout{ |
| 23 | + Create: schema.DefaultTimeout(timeout), |
| 24 | + Update: schema.DefaultTimeout(timeout), |
| 25 | + Read: schema.DefaultTimeout(timeout), |
| 26 | + Delete: schema.DefaultTimeout(timeout), |
| 27 | + }, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "no_mapping_strategy": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + "different_team_same_role_strategy": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func resourceSysdigGroupMappingConfigRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 42 | + client, err := m.(SysdigClients).sysdigCommonClientV2() |
| 43 | + if err != nil { |
| 44 | + return diag.FromErr(err) |
| 45 | + } |
| 46 | + |
| 47 | + groupMappingConfig, err := client.GetGroupMappingConfig(ctx) |
| 48 | + if err != nil { |
| 49 | + if err == v2.GroupMappingConfigNotFound { |
| 50 | + return nil |
| 51 | + } |
| 52 | + return diag.FromErr(err) |
| 53 | + } |
| 54 | + |
| 55 | + err = groupMappingConfigToResourceData(groupMappingConfig, d) |
| 56 | + if err != nil { |
| 57 | + return diag.FromErr(err) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func resourceSysdigGroupMappingConfigCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 64 | + client, err := m.(SysdigClients).sysdigCommonClientV2() |
| 65 | + if err != nil { |
| 66 | + return diag.FromErr(err) |
| 67 | + } |
| 68 | + |
| 69 | + groupMappingConfig := groupMappingConfigFromResourceData(d) |
| 70 | + _, err = client.UpdateGroupMappingConfig(ctx, groupMappingConfig) |
| 71 | + if err != nil { |
| 72 | + return diag.FromErr(err) |
| 73 | + } |
| 74 | + |
| 75 | + d.SetId("conflicts_resolution_strategies") |
| 76 | + |
| 77 | + resourceSysdigGroupMappingConfigRead(ctx, d, m) |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +func resourceSysdigGroupMappingConfigUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 83 | + client, err := m.(SysdigClients).sysdigCommonClientV2() |
| 84 | + if err != nil { |
| 85 | + return diag.FromErr(err) |
| 86 | + } |
| 87 | + |
| 88 | + groupMappingConfig := groupMappingConfigFromResourceData(d) |
| 89 | + _, err = client.UpdateGroupMappingConfig(ctx, groupMappingConfig) |
| 90 | + if err != nil { |
| 91 | + return diag.FromErr(err) |
| 92 | + } |
| 93 | + |
| 94 | + resourceSysdigGroupMappingConfigRead(ctx, d, m) |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func resourceSysdigGroupMappingConfigDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func groupMappingConfigToResourceData(groupMappingConfig *v2.GroupMappingConfig, d *schema.ResourceData) error { |
| 104 | + err := d.Set("no_mapping_strategy", groupMappingConfig.NoMappingStrategy) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + return d.Set("different_team_same_role_strategy", groupMappingConfig.DifferentTeamSameRoleStrategy) |
| 109 | +} |
| 110 | + |
| 111 | +func groupMappingConfigFromResourceData(d *schema.ResourceData) *v2.GroupMappingConfig { |
| 112 | + return &v2.GroupMappingConfig{ |
| 113 | + NoMappingStrategy: d.Get("no_mapping_strategy").(string), |
| 114 | + DifferentTeamSameRoleStrategy: d.Get("different_team_same_role_strategy").(string), |
| 115 | + } |
| 116 | +} |
0 commit comments