|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" |
| 11 | + cloudauth "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2/cloudauth/go" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 14 | +) |
| 15 | + |
| 16 | +func resourceSysdigSecureCloudauthAccountFeature() *schema.Resource { |
| 17 | + timeout := 5 * time.Minute |
| 18 | + |
| 19 | + return &schema.Resource{ |
| 20 | + CreateContext: resourceSysdigSecureCloudauthAccountFeatureCreate, |
| 21 | + UpdateContext: resourceSysdigSecureCloudauthAccountFeatureUpdate, |
| 22 | + ReadContext: resourceSysdigSecureCloudauthAccountFeatureRead, |
| 23 | + DeleteContext: resourceSysdigSecureCloudauthAccountFeatureDelete, |
| 24 | + Importer: &schema.ResourceImporter{ |
| 25 | + StateContext: schema.ImportStatePassthroughContext, |
| 26 | + }, |
| 27 | + Timeouts: &schema.ResourceTimeout{ |
| 28 | + Create: schema.DefaultTimeout(timeout), |
| 29 | + Update: schema.DefaultTimeout(timeout), |
| 30 | + Read: schema.DefaultTimeout(timeout), |
| 31 | + Delete: schema.DefaultTimeout(timeout), |
| 32 | + }, |
| 33 | + Schema: getAccountFeatureSchema(), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func getAccountFeatureSchema() map[string]*schema.Schema { |
| 38 | + // though the schema fields are already defined in cloud_auth_account resource, for AccountFeature |
| 39 | + // calls they are required fields. Also, account_id & flags are needed additionally. |
| 40 | + featureSchema := map[string]*schema.Schema{ |
| 41 | + SchemaAccountId: { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + }, |
| 45 | + SchemaType: { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + }, |
| 49 | + SchemaEnabled: { |
| 50 | + Type: schema.TypeBool, |
| 51 | + Required: true, |
| 52 | + }, |
| 53 | + SchemaComponents: { |
| 54 | + Type: schema.TypeList, |
| 55 | + Required: true, |
| 56 | + Elem: &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + }, |
| 59 | + }, |
| 60 | + SchemaFeatureFlags: { |
| 61 | + Type: schema.TypeMap, |
| 62 | + Optional: true, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + return featureSchema |
| 67 | +} |
| 68 | + |
| 69 | +func getSecureCloudauthAccountFeatureClient(client SysdigClients) (v2.CloudauthAccountFeatureSecureInterface, error) { |
| 70 | + return client.sysdigSecureClientV2() |
| 71 | +} |
| 72 | + |
| 73 | +func resourceSysdigSecureCloudauthAccountFeatureCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 74 | + client, err := getSecureCloudauthAccountFeatureClient((meta.(SysdigClients))) |
| 75 | + if err != nil { |
| 76 | + return diag.FromErr(err) |
| 77 | + } |
| 78 | + |
| 79 | + accountId := data.Get(SchemaAccountId).(string) |
| 80 | + cloudauthAccountFeature, errStatus, err := client.CreateOrUpdateCloudauthAccountFeatureSecure( |
| 81 | + ctx, accountId, data.Get(SchemaType).(string), cloudauthAccountFeatureFromResourceData(data)) |
| 82 | + if err != nil { |
| 83 | + return diag.Errorf("Error creating resource: %s %s", errStatus, err) |
| 84 | + } |
| 85 | + |
| 86 | + // using tuple 'accountId/featureType' as TF resource identifier |
| 87 | + data.SetId(accountId + "/" + cloudauthAccountFeature.GetType().String()) |
| 88 | + err = data.Set(SchemaAccountId, accountId) |
| 89 | + if err != nil { |
| 90 | + return diag.FromErr(err) |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func resourceSysdigSecureCloudauthAccountFeatureRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 97 | + client, err := getSecureCloudauthAccountFeatureClient((meta.(SysdigClients))) |
| 98 | + if err != nil { |
| 99 | + return diag.FromErr(err) |
| 100 | + } |
| 101 | + |
| 102 | + cloudauthAccountFeature, errStatus, err := client.GetCloudauthAccountFeatureSecure( |
| 103 | + ctx, data.Get(SchemaAccountId).(string), data.Get(SchemaType).(string)) |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + if strings.Contains(errStatus, "404") { |
| 107 | + return nil |
| 108 | + } |
| 109 | + return diag.Errorf("Error reading resource: %s %s", errStatus, err) |
| 110 | + } |
| 111 | + |
| 112 | + err = cloudauthAccountFeatureToResourceData(data, cloudauthAccountFeature) |
| 113 | + if err != nil { |
| 114 | + return diag.FromErr(err) |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func resourceSysdigSecureCloudauthAccountFeatureUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 121 | + client, err := getSecureCloudauthAccountFeatureClient(meta.(SysdigClients)) |
| 122 | + if err != nil { |
| 123 | + return diag.FromErr(err) |
| 124 | + } |
| 125 | + |
| 126 | + accountId := data.Get(SchemaAccountId).(string) |
| 127 | + existingCloudAccountFeature, errStatus, err := client.GetCloudauthAccountFeatureSecure( |
| 128 | + ctx, accountId, data.Get(SchemaType).(string)) |
| 129 | + if err != nil { |
| 130 | + if strings.Contains(errStatus, "404") { |
| 131 | + return nil |
| 132 | + } |
| 133 | + return diag.Errorf("Error reading resource: %s %s", errStatus, err) |
| 134 | + } |
| 135 | + |
| 136 | + newCloudAccountFeature := cloudauthAccountFeatureFromResourceData(data) |
| 137 | + |
| 138 | + // validate and reject non-updatable resource schema fields upfront |
| 139 | + err = validateCloudauthAccountFeatureUpdate(existingCloudAccountFeature, newCloudAccountFeature) |
| 140 | + if err != nil { |
| 141 | + return diag.Errorf("Error updating resource: %s", err) |
| 142 | + } |
| 143 | + |
| 144 | + _, errStatus, err = client.CreateOrUpdateCloudauthAccountFeatureSecure( |
| 145 | + ctx, accountId, data.Get(SchemaType).(string), newCloudAccountFeature) |
| 146 | + if err != nil { |
| 147 | + if strings.Contains(errStatus, "404") { |
| 148 | + return nil |
| 149 | + } |
| 150 | + return diag.Errorf("Error updating resource: %s %s", errStatus, err) |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func resourceSysdigSecureCloudauthAccountFeatureDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 157 | + client, err := getSecureCloudauthAccountFeatureClient(meta.(SysdigClients)) |
| 158 | + if err != nil { |
| 159 | + return diag.FromErr(err) |
| 160 | + } |
| 161 | + |
| 162 | + errStatus, err := client.DeleteCloudauthAccountFeatureSecure( |
| 163 | + ctx, data.Get(SchemaAccountId).(string), data.Get(SchemaType).(string)) |
| 164 | + if err != nil { |
| 165 | + if strings.Contains(errStatus, "404") { |
| 166 | + return nil |
| 167 | + } |
| 168 | + return diag.Errorf("Error deleting resource: %s %s", errStatus, err) |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +/* |
| 175 | +This function validates and restricts any fields not allowed to be updated during resource updates. |
| 176 | +*/ |
| 177 | +func validateCloudauthAccountFeatureUpdate(existingFeature *v2.CloudauthAccountFeatureSecure, newFeature *v2.CloudauthAccountFeatureSecure) error { |
| 178 | + if existingFeature.Type != newFeature.Type { |
| 179 | + errorInvalidResourceUpdate := fmt.Sprintf("Bad Request. Updating restricted fields not allowed: %s", []string{"type"}) |
| 180 | + return errors.New(errorInvalidResourceUpdate) |
| 181 | + } |
| 182 | + |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +func getFeatureComponentsList(data *schema.ResourceData) []string { |
| 187 | + componentsList := []string{} |
| 188 | + componentsResourceList := data.Get(SchemaComponents).([]interface{}) |
| 189 | + for _, componentID := range componentsResourceList { |
| 190 | + componentsList = append(componentsList, componentID.(string)) |
| 191 | + } |
| 192 | + return componentsList |
| 193 | +} |
| 194 | + |
| 195 | +func getFeatureFlags(data *schema.ResourceData) map[string]string { |
| 196 | + featureFlags := map[string]string{} |
| 197 | + flagsResource := data.Get(SchemaFeatureFlags).(map[string]interface{}) |
| 198 | + for name, value := range flagsResource { |
| 199 | + featureFlags[name] = value.(string) |
| 200 | + } |
| 201 | + return featureFlags |
| 202 | +} |
| 203 | + |
| 204 | +func cloudauthAccountFeatureFromResourceData(data *schema.ResourceData) *v2.CloudauthAccountFeatureSecure { |
| 205 | + cloudAccountFeature := &v2.CloudauthAccountFeatureSecure{ |
| 206 | + AccountFeature: cloudauth.AccountFeature{ |
| 207 | + Type: cloudauth.Feature(cloudauth.Feature_value[data.Get(SchemaType).(string)]), |
| 208 | + Enabled: data.Get(SchemaEnabled).(bool), |
| 209 | + Components: getFeatureComponentsList(data), |
| 210 | + Flags: getFeatureFlags(data), |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + return cloudAccountFeature |
| 215 | +} |
| 216 | + |
| 217 | +func cloudauthAccountFeatureToResourceData(data *schema.ResourceData, cloudAccountFeature *v2.CloudauthAccountFeatureSecure) error { |
| 218 | + |
| 219 | + accountId := data.Get(SchemaAccountId).(string) |
| 220 | + data.SetId(accountId + "/" + cloudAccountFeature.GetType().String()) |
| 221 | + |
| 222 | + err := data.Set(SchemaAccountId, accountId) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + |
| 227 | + err = data.Set(SchemaType, cloudAccountFeature.GetType().String()) |
| 228 | + if err != nil { |
| 229 | + return err |
| 230 | + } |
| 231 | + |
| 232 | + err = data.Set(SchemaEnabled, cloudAccountFeature.GetEnabled()) |
| 233 | + if err != nil { |
| 234 | + return err |
| 235 | + } |
| 236 | + |
| 237 | + err = data.Set(SchemaComponents, cloudAccountFeature.GetComponents()) |
| 238 | + if err != nil { |
| 239 | + return err |
| 240 | + } |
| 241 | + |
| 242 | + err = data.Set(SchemaFeatureFlags, cloudAccountFeature.GetFlags()) |
| 243 | + if err != nil { |
| 244 | + return err |
| 245 | + } |
| 246 | + |
| 247 | + return nil |
| 248 | +} |
0 commit comments