@go-to-k/cdkd 0.14.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/dist/index.js CHANGED
@@ -6399,6 +6399,60 @@ Error: ${err.message || "Unknown error"}`,
6399
6399
  }
6400
6400
  return resourceType.startsWith("AWS::");
6401
6401
  }
6402
+ /**
6403
+ * Adopt an already-deployed resource into cdkd state via Cloud Control API.
6404
+ *
6405
+ * Strategy: explicit-override only.
6406
+ * - With `knownPhysicalId` (from `--resource <id>=<physicalId>` or
6407
+ * `--resource-mapping`): call `GetResource(TypeName, Identifier)`,
6408
+ * parse `ResourceModel` (returned as a JSON string by CC API), and
6409
+ * return its keys as `attributes`.
6410
+ * - Without `knownPhysicalId`: return `null`. CC API has no efficient
6411
+ * `aws:cdk:path`-tag lookup — `ListResources` returns identifiers
6412
+ * only, so tag lookup would require one `GetResource` per resource
6413
+ * in the account, plus per-service tag-API calls (which CC API
6414
+ * doesn't expose uniformly). Cost vs. value isn't worth it; users
6415
+ * who need adoption for CC-API-only resource types should pass
6416
+ * `--resource <id>=<physicalId>` for those resources.
6417
+ *
6418
+ * SDK providers (S3, Lambda, IAM Role, etc.) implement their own
6419
+ * `import` with tag-based auto-lookup; this fallback only kicks in for
6420
+ * resource types that don't have a dedicated SDK provider.
6421
+ */
6422
+ async import(input) {
6423
+ if (!input.knownPhysicalId) {
6424
+ return null;
6425
+ }
6426
+ try {
6427
+ const resp = await this.cloudControlClient.send(
6428
+ new GetResourceCommand2({
6429
+ TypeName: input.resourceType,
6430
+ Identifier: input.knownPhysicalId
6431
+ })
6432
+ );
6433
+ let attributes = {};
6434
+ const raw = resp.ResourceDescription?.Properties;
6435
+ if (typeof raw === "string" && raw.length > 0) {
6436
+ try {
6437
+ const parsed = JSON.parse(raw);
6438
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
6439
+ attributes = parsed;
6440
+ }
6441
+ } catch (parseErr) {
6442
+ this.logger.debug(
6443
+ `Failed to parse CC API ResourceModel for ${input.resourceType}/${input.knownPhysicalId}: ${parseErr instanceof Error ? parseErr.message : String(parseErr)}`
6444
+ );
6445
+ }
6446
+ }
6447
+ return { physicalId: input.knownPhysicalId, attributes };
6448
+ } catch (error) {
6449
+ const err = error;
6450
+ if (err.name === "ResourceNotFoundException") {
6451
+ return null;
6452
+ }
6453
+ throw error;
6454
+ }
6455
+ }
6402
6456
  };
6403
6457
 
6404
6458
  // src/provisioning/providers/custom-resource-provider.ts
@@ -7063,6 +7117,8 @@ import {
7063
7117
  UntagRoleCommand,
7064
7118
  PutRolePermissionsBoundaryCommand,
7065
7119
  DeleteRolePermissionsBoundaryCommand,
7120
+ ListRolesCommand,
7121
+ ListRoleTagsCommand,
7066
7122
  NoSuchEntityException
7067
7123
  } from "@aws-sdk/client-iam";
7068
7124
  init_aws_clients();
@@ -7171,6 +7227,32 @@ function applyDefaultNameForFallback(logicalId, resourceType, properties) {
7171
7227
  };
7172
7228
  }
7173
7229
 
7230
+ // src/provisioning/import-helpers.ts
7231
+ function readNameProperty(input, propertyName) {
7232
+ const value = input.properties?.[propertyName];
7233
+ return typeof value === "string" && value.length > 0 ? value : void 0;
7234
+ }
7235
+ function resolveExplicitPhysicalId(input, nameProperty) {
7236
+ if (input.knownPhysicalId)
7237
+ return input.knownPhysicalId;
7238
+ if (nameProperty) {
7239
+ const name = readNameProperty(input, nameProperty);
7240
+ if (name)
7241
+ return name;
7242
+ }
7243
+ return void 0;
7244
+ }
7245
+ var CDK_PATH_TAG = "aws:cdk:path";
7246
+ function matchesCdkPath(tags, cdkPath) {
7247
+ if (!tags || !cdkPath)
7248
+ return false;
7249
+ for (const t of tags) {
7250
+ if (t.Key === CDK_PATH_TAG && t.Value === cdkPath)
7251
+ return true;
7252
+ }
7253
+ return false;
7254
+ }
7255
+
7174
7256
  // src/provisioning/providers/iam-role-provider.ts
7175
7257
  var IAMRoleProvider = class {
7176
7258
  iamClient;
@@ -7677,6 +7759,58 @@ var IAMRoleProvider = class {
7677
7759
  this.logger.debug(`Added/updated ${tagsToAdd.length} tags on role ${roleName}`);
7678
7760
  }
7679
7761
  }
7762
+ /**
7763
+ * Adopt an existing IAM role into cdkd state.
7764
+ *
7765
+ * Lookup order:
7766
+ * 1. `--resource` override or `Properties.RoleName` → use directly,
7767
+ * verify via `GetRole`.
7768
+ * 2. `ListRoles` + `ListRoleTags`, match `aws:cdk:path` tag.
7769
+ *
7770
+ * `ListRoles` is paginated and IAM is global (no region scoping), so this
7771
+ * walks every role in the account once. Acceptable for the cardinalities
7772
+ * we expect (typically <100 roles per account); larger accounts may want
7773
+ * to provide `--resource` overrides instead.
7774
+ */
7775
+ async import(input) {
7776
+ const explicit = resolveExplicitPhysicalId(input, "RoleName");
7777
+ if (explicit) {
7778
+ try {
7779
+ await this.iamClient.send(new GetRoleCommand({ RoleName: explicit }));
7780
+ return { physicalId: explicit, attributes: {} };
7781
+ } catch (err) {
7782
+ if (err instanceof NoSuchEntityException)
7783
+ return null;
7784
+ throw err;
7785
+ }
7786
+ }
7787
+ if (!input.cdkPath)
7788
+ return null;
7789
+ let marker;
7790
+ do {
7791
+ const list = await this.iamClient.send(
7792
+ new ListRolesCommand({ ...marker && { Marker: marker } })
7793
+ );
7794
+ for (const role of list.Roles ?? []) {
7795
+ if (!role.RoleName)
7796
+ continue;
7797
+ try {
7798
+ const tags = await this.iamClient.send(
7799
+ new ListRoleTagsCommand({ RoleName: role.RoleName })
7800
+ );
7801
+ if (matchesCdkPath(tags.Tags, input.cdkPath)) {
7802
+ return { physicalId: role.RoleName, attributes: {} };
7803
+ }
7804
+ } catch (err) {
7805
+ if (err instanceof NoSuchEntityException)
7806
+ continue;
7807
+ throw err;
7808
+ }
7809
+ }
7810
+ marker = list.IsTruncated ? list.Marker : void 0;
7811
+ } while (marker);
7812
+ return null;
7813
+ }
7680
7814
  };
7681
7815
 
7682
7816
  // src/deployment/dag-executor.ts