|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func dataSourceSysdigSecureZone() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + ReadContext: dataSourceSysdigSecureZoneRead, |
| 17 | + |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + SchemaDescriptionKey: { |
| 20 | + Type: schema.TypeString, |
| 21 | + Computed: true, |
| 22 | + }, |
| 23 | + SchemaIsSystemKey: { |
| 24 | + Type: schema.TypeBool, |
| 25 | + Computed: true, |
| 26 | + }, |
| 27 | + SchemaAuthorKey: { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + SchemaLastModifiedBy: { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + SchemaLastUpdated: { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + SchemaScopeKey: { |
| 40 | + Type: schema.TypeSet, |
| 41 | + Computed: true, |
| 42 | + Elem: &schema.Resource{ |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + SchemaIDKey: { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + SchemaTargetTypeKey: { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + SchemaRulesKey: { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + "id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + ExactlyOneOf: []string{"id", "name"}, |
| 63 | + Description: "The ID of the zone to retrieve.", |
| 64 | + }, |
| 65 | + "name": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Optional: true, |
| 68 | + ExactlyOneOf: []string{"id", "name"}, |
| 69 | + Description: "The name of the zone to retrieve.", |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func dataSourceSysdigSecureZoneRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 76 | + client, err := getZoneClient(m.(SysdigClients)) |
| 77 | + if err != nil { |
| 78 | + return diag.FromErr(err) |
| 79 | + } |
| 80 | + |
| 81 | + var zone *v2.Zone |
| 82 | + zoneIDRaw, hasZoneID := d.GetOk("id") |
| 83 | + if hasZoneID { |
| 84 | + zoneID, err := strconv.Atoi(zoneIDRaw.(string)) |
| 85 | + if err != nil { |
| 86 | + return diag.FromErr(fmt.Errorf("invalid zone id: %s", err)) |
| 87 | + } |
| 88 | + zone, err = client.GetZoneById(ctx, zoneID) |
| 89 | + if err != nil { |
| 90 | + return diag.FromErr(fmt.Errorf("error fetching zone by ID: %s", err)) |
| 91 | + } |
| 92 | + } else if nameRaw, hasName := d.GetOk("name"); hasName { |
| 93 | + name := nameRaw.(string) |
| 94 | + zones, err := client.GetZones(ctx, name) |
| 95 | + if err != nil { |
| 96 | + return diag.FromErr(fmt.Errorf("error fetching zones: %s", err)) |
| 97 | + } |
| 98 | + for _, z := range zones { |
| 99 | + if z.Name == name { |
| 100 | + zone = &z |
| 101 | + break |
| 102 | + } |
| 103 | + } |
| 104 | + if zone == nil { |
| 105 | + return diag.FromErr(fmt.Errorf("zone with name '%s' not found", name)) |
| 106 | + } |
| 107 | + } else { |
| 108 | + return diag.FromErr(fmt.Errorf("either id or name must be specified")) |
| 109 | + } |
| 110 | + |
| 111 | + d.SetId(fmt.Sprintf("%d", zone.ID)) |
| 112 | + _ = d.Set(SchemaNameKey, zone.Name) |
| 113 | + _ = d.Set(SchemaDescriptionKey, zone.Description) |
| 114 | + _ = d.Set(SchemaIsSystemKey, zone.IsSystem) |
| 115 | + _ = d.Set(SchemaAuthorKey, zone.Author) |
| 116 | + _ = d.Set(SchemaLastModifiedBy, zone.LastModifiedBy) |
| 117 | + _ = d.Set(SchemaLastUpdated, time.UnixMilli(zone.LastUpdated).Format(time.RFC3339)) |
| 118 | + |
| 119 | + if err := d.Set(SchemaScopeKey, fromZoneScopesResponse(zone.Scopes)); err != nil { |
| 120 | + return diag.FromErr(fmt.Errorf("error setting scope: %s", err)) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
0 commit comments