|
| 1 | +// Copyright (c) Mondoo, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package resources |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + backupdr "cloud.google.com/go/backupdr/apiv1" |
| 12 | + "cloud.google.com/go/backupdr/apiv1/backupdrpb" |
| 13 | + "go.mondoo.com/mql/v13/llx" |
| 14 | + "go.mondoo.com/mql/v13/providers-sdk/v1/plugin" |
| 15 | + "go.mondoo.com/mql/v13/providers-sdk/v1/util/convert" |
| 16 | + "go.mondoo.com/mql/v13/providers/gcp/connection" |
| 17 | + "go.mondoo.com/mql/v13/types" |
| 18 | + "google.golang.org/api/iterator" |
| 19 | + "google.golang.org/api/option" |
| 20 | +) |
| 21 | + |
| 22 | +func (g *mqlGcpProject) backupdr() (*mqlGcpProjectBackupdrService, error) { |
| 23 | + if g.Id.Error != nil { |
| 24 | + return nil, g.Id.Error |
| 25 | + } |
| 26 | + res, err := CreateResource(g.MqlRuntime, "gcp.project.backupdrService", map[string]*llx.RawData{ |
| 27 | + "projectId": llx.StringData(g.Id.Data), |
| 28 | + }) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + return res.(*mqlGcpProjectBackupdrService), nil |
| 33 | +} |
| 34 | + |
| 35 | +func initGcpProjectBackupdrService(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) { |
| 36 | + if len(args) > 0 { |
| 37 | + return args, nil, nil |
| 38 | + } |
| 39 | + conn, ok := runtime.Connection.(*connection.GcpConnection) |
| 40 | + if !ok { |
| 41 | + return nil, nil, errors.New("invalid connection provided, it is not a GCP connection") |
| 42 | + } |
| 43 | + args["projectId"] = llx.StringData(conn.ResourceID()) |
| 44 | + return args, nil, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (g *mqlGcpProjectBackupdrService) id() (string, error) { |
| 48 | + if g.ProjectId.Error != nil { |
| 49 | + return "", g.ProjectId.Error |
| 50 | + } |
| 51 | + return fmt.Sprintf("gcp.project/%s/backupdrService", g.ProjectId.Data), nil |
| 52 | +} |
| 53 | + |
| 54 | +func (g *mqlGcpProjectBackupdrService) managementServers() ([]any, error) { |
| 55 | + if g.ProjectId.Error != nil { |
| 56 | + return nil, g.ProjectId.Error |
| 57 | + } |
| 58 | + projectId := g.ProjectId.Data |
| 59 | + |
| 60 | + conn := g.MqlRuntime.Connection.(*connection.GcpConnection) |
| 61 | + creds, err := conn.Credentials(backupdr.DefaultAuthScopes()...) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + ctx := context.Background() |
| 67 | + client, err := backupdr.NewClient(ctx, option.WithCredentials(creds)) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + defer client.Close() |
| 72 | + |
| 73 | + it := client.ListManagementServers(ctx, &backupdrpb.ListManagementServersRequest{ |
| 74 | + Parent: fmt.Sprintf("projects/%s/locations/-", projectId), |
| 75 | + }) |
| 76 | + |
| 77 | + var res []any |
| 78 | + for { |
| 79 | + server, err := it.Next() |
| 80 | + if err == iterator.Done { |
| 81 | + break |
| 82 | + } |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + networks := make([]any, 0, len(server.Networks)) |
| 88 | + for _, n := range server.Networks { |
| 89 | + d, err := protoToDict(n) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + networks = append(networks, d) |
| 94 | + } |
| 95 | + |
| 96 | + managementUri, err := protoToDict(server.ManagementUri) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + mqlServer, err := CreateResource(g.MqlRuntime, "gcp.project.backupdrService.managementServer", map[string]*llx.RawData{ |
| 102 | + "name": llx.StringData(server.Name), |
| 103 | + "description": llx.StringData(server.Description), |
| 104 | + "state": llx.StringData(server.State.String()), |
| 105 | + "type": llx.StringData(server.Type.String()), |
| 106 | + "networks": llx.ArrayData(networks, types.Dict), |
| 107 | + "managementUri": llx.DictData(managementUri), |
| 108 | + "oauth2ClientId": llx.StringData(server.Oauth2ClientId), |
| 109 | + "satisfiesPzs": llx.BoolData(server.GetSatisfiesPzs().GetValue()), |
| 110 | + "etag": llx.StringData(server.Etag), |
| 111 | + "labels": llx.MapData(convert.MapToInterfaceMap(server.Labels), types.String), |
| 112 | + "createdAt": llx.TimeDataPtr(timestampAsTimePtr(server.CreateTime)), |
| 113 | + "updatedAt": llx.TimeDataPtr(timestampAsTimePtr(server.UpdateTime)), |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + res = append(res, mqlServer) |
| 119 | + } |
| 120 | + return res, nil |
| 121 | +} |
| 122 | + |
| 123 | +func (g *mqlGcpProjectBackupdrServiceManagementServer) id() (string, error) { |
| 124 | + return g.Name.Data, g.Name.Error |
| 125 | +} |
| 126 | + |
| 127 | +func (g *mqlGcpProjectBackupdrService) backupVaults() ([]any, error) { |
| 128 | + if g.ProjectId.Error != nil { |
| 129 | + return nil, g.ProjectId.Error |
| 130 | + } |
| 131 | + projectId := g.ProjectId.Data |
| 132 | + |
| 133 | + conn := g.MqlRuntime.Connection.(*connection.GcpConnection) |
| 134 | + creds, err := conn.Credentials(backupdr.DefaultAuthScopes()...) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + ctx := context.Background() |
| 140 | + client, err := backupdr.NewClient(ctx, option.WithCredentials(creds)) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + defer client.Close() |
| 145 | + |
| 146 | + it := client.ListBackupVaults(ctx, &backupdrpb.ListBackupVaultsRequest{ |
| 147 | + Parent: fmt.Sprintf("projects/%s/locations/-", projectId), |
| 148 | + }) |
| 149 | + |
| 150 | + var res []any |
| 151 | + for { |
| 152 | + vault, err := it.Next() |
| 153 | + if err == iterator.Done { |
| 154 | + break |
| 155 | + } |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + |
| 160 | + mqlVault, err := CreateResource(g.MqlRuntime, "gcp.project.backupdrService.backupVault", map[string]*llx.RawData{ |
| 161 | + "name": llx.StringData(vault.Name), |
| 162 | + "description": llx.StringData(vault.GetDescription()), |
| 163 | + "state": llx.StringData(vault.State.String()), |
| 164 | + "backupMinimumEnforcedRetentionDuration": llx.StringData(vault.BackupMinimumEnforcedRetentionDuration.String()), |
| 165 | + "deletable": llx.BoolData(vault.GetDeletable()), |
| 166 | + "etag": llx.StringData(vault.GetEtag()), |
| 167 | + "effectiveTime": llx.TimeDataPtr(timestampAsTimePtr(vault.EffectiveTime)), |
| 168 | + "backupCount": llx.IntData(vault.GetBackupCount()), |
| 169 | + "serviceAccount": llx.StringData(vault.ServiceAccount), |
| 170 | + "totalStoredBytes": llx.IntData(vault.GetTotalStoredBytes()), |
| 171 | + "accessRestriction": llx.StringData(vault.AccessRestriction.String()), |
| 172 | + "annotations": llx.MapData(convert.MapToInterfaceMap(vault.Annotations), types.String), |
| 173 | + "labels": llx.MapData(convert.MapToInterfaceMap(vault.Labels), types.String), |
| 174 | + "createdAt": llx.TimeDataPtr(timestampAsTimePtr(vault.CreateTime)), |
| 175 | + "updatedAt": llx.TimeDataPtr(timestampAsTimePtr(vault.UpdateTime)), |
| 176 | + }) |
| 177 | + if err != nil { |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + res = append(res, mqlVault) |
| 181 | + } |
| 182 | + return res, nil |
| 183 | +} |
| 184 | + |
| 185 | +func (g *mqlGcpProjectBackupdrServiceBackupVault) id() (string, error) { |
| 186 | + return g.Name.Data, g.Name.Error |
| 187 | +} |
| 188 | + |
| 189 | +func (g *mqlGcpProjectBackupdrServiceBackupVault) dataSources() ([]any, error) { |
| 190 | + if g.Name.Error != nil { |
| 191 | + return nil, g.Name.Error |
| 192 | + } |
| 193 | + vaultName := g.Name.Data |
| 194 | + |
| 195 | + conn := g.MqlRuntime.Connection.(*connection.GcpConnection) |
| 196 | + creds, err := conn.Credentials(backupdr.DefaultAuthScopes()...) |
| 197 | + if err != nil { |
| 198 | + return nil, err |
| 199 | + } |
| 200 | + |
| 201 | + ctx := context.Background() |
| 202 | + client, err := backupdr.NewClient(ctx, option.WithCredentials(creds)) |
| 203 | + if err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + defer client.Close() |
| 207 | + |
| 208 | + it := client.ListDataSources(ctx, &backupdrpb.ListDataSourcesRequest{ |
| 209 | + Parent: vaultName, |
| 210 | + }) |
| 211 | + |
| 212 | + var res []any |
| 213 | + for { |
| 214 | + ds, err := it.Next() |
| 215 | + if err == iterator.Done { |
| 216 | + break |
| 217 | + } |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + |
| 222 | + gcpResource, err := protoToDict(ds.GetDataSourceGcpResource()) |
| 223 | + if err != nil { |
| 224 | + return nil, err |
| 225 | + } |
| 226 | + backupApplianceApp, err := protoToDict(ds.GetDataSourceBackupApplianceApplication()) |
| 227 | + if err != nil { |
| 228 | + return nil, err |
| 229 | + } |
| 230 | + |
| 231 | + mqlDs, err := CreateResource(g.MqlRuntime, "gcp.project.backupdrService.dataSource", map[string]*llx.RawData{ |
| 232 | + "name": llx.StringData(ds.Name), |
| 233 | + "state": llx.StringData(ds.State.String()), |
| 234 | + "labels": llx.MapData(convert.MapToInterfaceMap(ds.Labels), types.String), |
| 235 | + "dataSourceGcpResource": llx.DictData(gcpResource), |
| 236 | + "dataSourceBackupApplianceApplication": llx.DictData(backupApplianceApp), |
| 237 | + "totalStoredBytes": llx.IntData(ds.GetTotalStoredBytes()), |
| 238 | + "backupCount": llx.IntData(ds.GetBackupCount()), |
| 239 | + "etag": llx.StringData(ds.GetEtag()), |
| 240 | + "configState": llx.StringData(ds.ConfigState.String()), |
| 241 | + "createdAt": llx.TimeDataPtr(timestampAsTimePtr(ds.CreateTime)), |
| 242 | + "updatedAt": llx.TimeDataPtr(timestampAsTimePtr(ds.UpdateTime)), |
| 243 | + }) |
| 244 | + if err != nil { |
| 245 | + return nil, err |
| 246 | + } |
| 247 | + res = append(res, mqlDs) |
| 248 | + } |
| 249 | + return res, nil |
| 250 | +} |
| 251 | + |
| 252 | +func (g *mqlGcpProjectBackupdrServiceDataSource) id() (string, error) { |
| 253 | + return g.Name.Data, g.Name.Error |
| 254 | +} |
| 255 | + |
| 256 | +func (g *mqlGcpProjectBackupdrService) backupPlans() ([]any, error) { |
| 257 | + if g.ProjectId.Error != nil { |
| 258 | + return nil, g.ProjectId.Error |
| 259 | + } |
| 260 | + projectId := g.ProjectId.Data |
| 261 | + |
| 262 | + conn := g.MqlRuntime.Connection.(*connection.GcpConnection) |
| 263 | + creds, err := conn.Credentials(backupdr.DefaultAuthScopes()...) |
| 264 | + if err != nil { |
| 265 | + return nil, err |
| 266 | + } |
| 267 | + |
| 268 | + ctx := context.Background() |
| 269 | + client, err := backupdr.NewClient(ctx, option.WithCredentials(creds)) |
| 270 | + if err != nil { |
| 271 | + return nil, err |
| 272 | + } |
| 273 | + defer client.Close() |
| 274 | + |
| 275 | + it := client.ListBackupPlans(ctx, &backupdrpb.ListBackupPlansRequest{ |
| 276 | + Parent: fmt.Sprintf("projects/%s/locations/-", projectId), |
| 277 | + }) |
| 278 | + |
| 279 | + var res []any |
| 280 | + for { |
| 281 | + plan, err := it.Next() |
| 282 | + if err == iterator.Done { |
| 283 | + break |
| 284 | + } |
| 285 | + if err != nil { |
| 286 | + return nil, err |
| 287 | + } |
| 288 | + |
| 289 | + backupRules := make([]any, 0, len(plan.BackupRules)) |
| 290 | + for _, rule := range plan.BackupRules { |
| 291 | + d, err := protoToDict(rule) |
| 292 | + if err != nil { |
| 293 | + return nil, err |
| 294 | + } |
| 295 | + backupRules = append(backupRules, d) |
| 296 | + } |
| 297 | + |
| 298 | + mqlPlan, err := CreateResource(g.MqlRuntime, "gcp.project.backupdrService.backupPlan", map[string]*llx.RawData{ |
| 299 | + "name": llx.StringData(plan.Name), |
| 300 | + "description": llx.StringData(plan.Description), |
| 301 | + "state": llx.StringData(plan.State.String()), |
| 302 | + "resourceType": llx.StringData(plan.ResourceType), |
| 303 | + "backupVault": llx.StringData(plan.BackupVault), |
| 304 | + "backupVaultServiceAccount": llx.StringData(plan.BackupVaultServiceAccount), |
| 305 | + "labels": llx.MapData(convert.MapToInterfaceMap(plan.Labels), types.String), |
| 306 | + "backupRules": llx.ArrayData(backupRules, types.Dict), |
| 307 | + "etag": llx.StringData(plan.Etag), |
| 308 | + "createdAt": llx.TimeDataPtr(timestampAsTimePtr(plan.CreateTime)), |
| 309 | + "updatedAt": llx.TimeDataPtr(timestampAsTimePtr(plan.UpdateTime)), |
| 310 | + }) |
| 311 | + if err != nil { |
| 312 | + return nil, err |
| 313 | + } |
| 314 | + res = append(res, mqlPlan) |
| 315 | + } |
| 316 | + return res, nil |
| 317 | +} |
| 318 | + |
| 319 | +func (g *mqlGcpProjectBackupdrServiceBackupPlan) id() (string, error) { |
| 320 | + return g.Name.Data, g.Name.Error |
| 321 | +} |
0 commit comments