|
50 | 50 | use Utopia\Migration\Resources\Functions\Deployment; |
51 | 51 | use Utopia\Migration\Resources\Functions\EnvVar; |
52 | 52 | use Utopia\Migration\Resources\Functions\Func; |
| 53 | +use Utopia\Migration\Resources\Integrations\ApiKey; |
53 | 54 | use Utopia\Migration\Resources\Integrations\Platform; |
54 | 55 | use Utopia\Migration\Resources\Messaging\Message; |
55 | 56 | use Utopia\Migration\Resources\Messaging\Provider; |
@@ -275,6 +276,7 @@ public static function getSupportedResources(): array |
275 | 276 |
|
276 | 277 | // Integrations |
277 | 278 | Resource::TYPE_PLATFORM, |
| 279 | + Resource::TYPE_API_KEY, |
278 | 280 |
|
279 | 281 | // Backups |
280 | 282 | Resource::TYPE_BACKUP_POLICY, |
@@ -3074,6 +3076,10 @@ public function importIntegrationsResource(Resource $resource): Resource |
3074 | 3076 | /** @var Platform $resource */ |
3075 | 3077 | $this->createPlatform($resource); |
3076 | 3078 | break; |
| 3079 | + case Resource::TYPE_API_KEY: |
| 3080 | + /** @var ApiKey $resource */ |
| 3081 | + $this->createApiKey($resource); |
| 3082 | + break; |
3077 | 3083 | } |
3078 | 3084 |
|
3079 | 3085 | if ($resource->getStatus() !== Resource::STATUS_SKIPPED) { |
@@ -3126,6 +3132,49 @@ protected function createPlatform(Platform $resource): bool |
3126 | 3132 | return true; |
3127 | 3133 | } |
3128 | 3134 |
|
| 3135 | + protected function createApiKey(ApiKey $resource): bool |
| 3136 | + { |
| 3137 | + $existing = $this->dbForPlatform->findOne('keys', [ |
| 3138 | + Query::equal('resourceType', ['projects']), |
| 3139 | + Query::equal('resourceInternalId', [$this->projectInternalId]), |
| 3140 | + Query::equal('name', [$resource->getApiKeyName()]), |
| 3141 | + ]); |
| 3142 | + |
| 3143 | + if ($existing !== false && !$existing->isEmpty()) { |
| 3144 | + $resource->setStatus(Resource::STATUS_SKIPPED, 'API key already exists'); |
| 3145 | + return false; |
| 3146 | + } |
| 3147 | + |
| 3148 | + $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); |
| 3149 | + $updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt); |
| 3150 | + $expire = $resource->getExpire(); |
| 3151 | + |
| 3152 | + try { |
| 3153 | + $this->dbForPlatform->createDocument('keys', new UtopiaDocument([ |
| 3154 | + '$id' => ID::unique(), |
| 3155 | + '$permissions' => $resource->getPermissions(), |
| 3156 | + 'resourceInternalId' => $this->projectInternalId, |
| 3157 | + 'resourceId' => $this->project, |
| 3158 | + 'resourceType' => 'projects', |
| 3159 | + 'name' => $resource->getApiKeyName(), |
| 3160 | + 'scopes' => $resource->getScopes(), |
| 3161 | + 'expire' => empty($expire) ? null : $expire, |
| 3162 | + 'sdks' => $resource->getSdks(), |
| 3163 | + 'accessedAt' => null, |
| 3164 | + 'secret' => 'standard_' . \bin2hex(\random_bytes(128)), |
| 3165 | + '$createdAt' => $createdAt, |
| 3166 | + '$updatedAt' => $updatedAt, |
| 3167 | + ])); |
| 3168 | + } catch (DuplicateException) { |
| 3169 | + $resource->setStatus(Resource::STATUS_SKIPPED, 'API key already exists'); |
| 3170 | + return false; |
| 3171 | + } |
| 3172 | + |
| 3173 | + $this->dbForPlatform->purgeCachedDocument('projects', $this->project); |
| 3174 | + |
| 3175 | + return true; |
| 3176 | + } |
| 3177 | + |
3129 | 3178 | private function validateFieldsForIndexes(Index $resource, UtopiaDocument $table, array &$lengths) |
3130 | 3179 | { |
3131 | 3180 | /** |
|
0 commit comments