/** * Create the dynamodb table before tests run. */ protected static function createTable() { try { self::$client->describeTable(['TableName' => self::$tableName]); return; } catch (DynamoDbException $e) { // table doesn't exist, create it below } self::$client->createTable(['TableName' => self::$tableName, 'AttributeDefinitions' => [['AttributeName' => 'id', 'AttributeType' => 'S']], 'KeySchema' => array(['AttributeName' => 'id', 'KeyType' => 'HASH']), 'ProvisionedThroughput' => ['ReadCapacityUnits' => 5, 'WriteCapacityUnits' => 5]]); self::$client->waitUntil('TableExists', ['TableName' => self::$tableName]); }
/** * Create the table */ protected static function createTable($wait = false) { $tables = self::$dynamodb->listTables(); if (in_array(self::TABLE_NAME, $tables['TableNames'])) { self::deleteTable(true); } self::$dynamodb->createTable(['TableName' => self::TABLE_NAME, 'AttributeDefinitions' => [['AttributeName' => 'key', 'AttributeType' => 'S']], 'KeySchema' => [['AttributeName' => 'key', 'KeyType' => 'HASH']], 'ProvisionedThroughput' => ['ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1]]); if (!$wait) { return; } self::$dynamodb->waitUntil('TableExists', ['TableName' => self::TABLE_NAME, 'waiter.interval' => 1, 'waiter.max_attempts' => 5]); }
/** * @param $tableName * @param DynamoDbIndex $primaryIndex * @param DynamoDbIndex[] $localSecondaryIndices * @param DynamoDbIndex[] $globalSecondaryIndices * @param int $readCapacity * @param int $writeCapacity * * @return bool * @internal param DynamoDbIndex $primaryKey */ public function createTable($tableName, DynamoDbIndex $primaryIndex, array $localSecondaryIndices = [], array $globalSecondaryIndices = [], $readCapacity = 5, $writeCapacity = 5) { $attrDef = $primaryIndex->getAttributeDefinitions(); foreach ($globalSecondaryIndices as $gsi) { $gsiDef = $gsi->getAttributeDefinitions(); $attrDef = array_merge($attrDef, $gsiDef); } foreach ($localSecondaryIndices as $lsi) { $lsiDef = $lsi->getAttributeDefinitions(); $attrDef = array_merge($attrDef, $lsiDef); } $attrDef = array_values($attrDef); $keySchema = $primaryIndex->getKeySchema(); $gsiDef = []; foreach ($globalSecondaryIndices as $globalSecondaryIndex) { $gsiDef[] = ["IndexName" => $globalSecondaryIndex->getName(), "KeySchema" => $globalSecondaryIndex->getKeySchema(), "Projection" => $globalSecondaryIndex->getProjection(), "ProvisionedThroughput" => ["ReadCapacityUnits" => $readCapacity, "WriteCapacityUnits" => $writeCapacity]]; } $lsiDef = []; foreach ($localSecondaryIndices as $localSecondaryIndex) { $lsiDef[] = ["IndexName" => $localSecondaryIndex->getName(), "KeySchema" => $localSecondaryIndex->getKeySchema(), "Projection" => $localSecondaryIndex->getProjection()]; } $args = ["TableName" => $tableName, "ProvisionedThroughput" => ["ReadCapacityUnits" => $readCapacity, "WriteCapacityUnits" => $writeCapacity], "AttributeDefinitions" => $attrDef, "KeySchema" => $keySchema]; if ($gsiDef) { $args["GlobalSecondaryIndexes"] = $gsiDef; } if ($lsiDef) { $args["LocalSecondaryIndexes"] = $lsiDef; } $result = $this->db->createTable($args); if (isset($result['TableDescription']) && $result['TableDescription']) { return true; } else { return false; } }
/** * @param string $tableName */ protected function createTable($tableName) { try { $this->client->describeTable(['TableName' => $tableName]); } catch (ResourceNotFoundException $e) { $this->client->createTable(['AttributeDefinitions' => [['AttributeName' => 'id', 'AttributeType' => 'S']], 'TableName' => $tableName, 'KeySchema' => [['AttributeName' => 'id', 'KeyType' => 'HASH']], 'ProvisionedThroughput' => ['ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1]]); } }
protected function execute(InputInterface $input, OutputInterface $output) { $this->dynamoDbClient = $this->getContainer()->get('queue.dynamodb_client'); $this->tableName = $this->getContainer()->getParameter('container_stats_dynamodb.table_name'); try { $this->dynamoDbClient->describeTable(['TableName' => $this->tableName]); echo 'Table ' . $this->tableName . ' already exists.' . "\n"; } catch (DynamoDbException $e) { if (strpos($e->getMessage(), 'ResourceNotFoundException') !== false) { echo 'Creating ' . $this->tableName . ' table ...'; $this->dynamoDbClient->createTable($this->getTableConfiguration()); $this->dynamoDbClient->waitUntil('TableExists', ['TableName' => $this->tableName, '@waiter' => ['delay' => 3, 'maxAttempts' => 20]]); echo ' done!' . "\n"; } else { throw $e; } } }
/** * Executes the CreateTable operation. * * @param string $tableName The name of the table to create. * @param array $attributeDefinitions An array of attributes that describe the key schema for the table and indexes. * @param array $keySchema Specifies the attributes that make up the primary key for a table or an index * @param array $provisionedThroughput Represents the provisioned throughput settings for a specified table or index. * @param array $localSecondaryIndexes One or more local secondary indexes (the maximum is five) to be created on the table. * @param array $globalSecondaryIndexes One or more global secondary indexes (the maximum is five) to be created on the table. * * @return Guzzle\Service\Resource\Model * * @see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_createTable */ public function createTable($tableName, array $attributeDefinitions, array $keySchema, array $provisionedThroughput, array $localSecondaryIndexes = null, array $globalSecondaryIndexes = null) { $args = ['AttributeDefinitions' => $attributeDefinitions, 'TableName' => $tableName, 'KeySchema' => $keySchema, 'ProvisionedThroughput' => $provisionedThroughput]; if ($localSecondaryIndexes !== null) { $args['LocalSecondaryIndexes'] = $localSecondaryIndexes; } if ($globalSecondaryIndexes !== null) { $args['GlobalSecondaryIndexes'] = $globalSecondaryIndexes; } return $this->client->createTable($args); }
/** * Create table via the create_table call * @param string $table The name of the table * @param Table\KeySchema $keySchama * @param Table\ProvisionedThroughput $provisionedThroughput * @return Table\TableDescription */ public function createTable($table, Table\KeySchema $keySchama, Table\ProvisionedThroughput $provisionedThroughput) { if (null !== $this->logger) { $this->log('Create table ' . $table); } $parameters = array('TableName' => $table, 'KeySchema' => $keySchama->getForDynamoDB(), 'ProvisionedThroughput' => $provisionedThroughput->getForDynamoDB()); if (null !== $this->logger) { $this->log('TableCreate request paramaters : ' . print_r($parameters, true), Logger::DEBUG); } $response = $this->connector->createTable($parameters); if (null !== $this->logger) { $this->log('TableCreate request response : ' . print_r($response, true), Logger::DEBUG); } }
/** * Ensures that a DynamoDB table can be created */ public function testCreatesTable() { self::log("Waiting until {$this->table} does not exist"); $this->client->waitUntil('TableNotExists', $this->table); self::log("Attempting to create {$this->table}"); $this->client->createTable(array('TableName' => $this->table, 'KeySchema' => array('HashKeyElement' => array('AttributeName' => 'foo', 'AttributeType' => 'S'), 'RangeKeyElement' => array('AttributeName' => 'bar', 'AttributeType' => 'N')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 10, 'WriteCapacityUnits' => 10))); // Check/wait until the table exists self::log("Table created. Waiting until it exists."); $this->client->waitUntil('TableExists', $this->table); self::log("Table exists"); // Ensure that the fields were set properly $result = $this->client->describeTable(array('TableName' => $this->table)); self::log("Ensuring the table was created with the proper values"); $this->assertEquals($this->table, $result['Table']['TableName']); $this->assertEquals('foo', $result['Table']['KeySchema']['HashKeyElement']['AttributeName']); $this->assertEquals('S', $result['Table']['KeySchema']['HashKeyElement']['AttributeType']); $this->assertEquals('bar', $result['Table']['KeySchema']['RangeKeyElement']['AttributeName']); $this->assertEquals('N', $result['Table']['KeySchema']['RangeKeyElement']['AttributeType']); $this->assertEquals(10, $result['Table']['ProvisionedThroughput']['ReadCapacityUnits']); $this->assertEquals(10, $result['Table']['ProvisionedThroughput']['WriteCapacityUnits']); }
private function createDynamoDb(\Aws\DynamoDb\DynamoDbClient $client, $prefix = null) { $tablesList = explode(' ', 'oauth_access_tokens oauth_authorization_codes oauth_clients oauth_jwt oauth_public_keys oauth_refresh_tokens oauth_scopes oauth_users'); $nbTables = count($tablesList); $client->createTable(array('TableName' => $prefix . 'oauth_access_tokens', 'AttributeDefinitions' => array(array('AttributeName' => 'access_token', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'access_token', 'KeyType' => 'HASH')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); $client->createTable(array('TableName' => $prefix . 'oauth_authorization_codes', 'AttributeDefinitions' => array(array('AttributeName' => 'authorization_code', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'authorization_code', 'KeyType' => 'HASH')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); $client->createTable(array('TableName' => $prefix . 'oauth_clients', 'AttributeDefinitions' => array(array('AttributeName' => 'client_id', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'client_id', 'KeyType' => 'HASH')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); $client->createTable(array('TableName' => $prefix . 'oauth_jwt', 'AttributeDefinitions' => array(array('AttributeName' => 'client_id', 'AttributeType' => 'S'), array('AttributeName' => 'subject', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'client_id', 'KeyType' => 'HASH'), array('AttributeName' => 'subject', 'KeyType' => 'RANGE')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); $client->createTable(array('TableName' => $prefix . 'oauth_public_keys', 'AttributeDefinitions' => array(array('AttributeName' => 'client_id', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'client_id', 'KeyType' => 'HASH')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); $client->createTable(array('TableName' => $prefix . 'oauth_refresh_tokens', 'AttributeDefinitions' => array(array('AttributeName' => 'refresh_token', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'refresh_token', 'KeyType' => 'HASH')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); $client->createTable(array('TableName' => $prefix . 'oauth_scopes', 'AttributeDefinitions' => array(array('AttributeName' => 'scope', 'AttributeType' => 'S'), array('AttributeName' => 'is_default', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'scope', 'KeyType' => 'HASH')), 'GlobalSecondaryIndexes' => array(array('IndexName' => 'is_default-index', 'KeySchema' => array(array('AttributeName' => 'is_default', 'KeyType' => 'HASH')), 'Projection' => array('ProjectionType' => 'ALL'), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); $client->createTable(array('TableName' => $prefix . 'oauth_users', 'AttributeDefinitions' => array(array('AttributeName' => 'username', 'AttributeType' => 'S')), 'KeySchema' => array(array('AttributeName' => 'username', 'KeyType' => 'HASH')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1))); // Wait for creation $nbTableCreated = 0; while ($nbTableCreated != $nbTables) { $nbTableCreated = 0; foreach ($tablesList as $key => $table) { try { $result = $client->describeTable(array('TableName' => $prefix . $table)); if ($result['Table']['TableStatus'] == 'ACTIVE') { $nbTableCreated++; } } catch (\Aws\DynamoDb\Exception\DynamoDbException $e) { // Table does not exist : nothing to do $nbTableCreated++; } } if ($nbTableCreated != $nbTables) { sleep(1); } } }