forked from aws/aws-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaSetup.cpp
More file actions
212 lines (175 loc) · 7.23 KB
/
SchemaSetup.cpp
File metadata and controls
212 lines (175 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <SchemaSetup.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/utils/Outcome.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/dynamodb/DynamoDBClient.h>
#include <aws/dynamodb/DynamoDBErrors.h>
#include <aws/dynamodb/model/CreateTableRequest.h>
#include <aws/dynamodb/model/CreateTableResult.h>
#include <aws/dynamodb/model/DeleteTableRequest.h>
#include <aws/dynamodb/model/DeleteTableResult.h>
#include <aws/dynamodb/model/DescribeTableRequest.h>
#include <aws/dynamodb/model/DescribeTableResult.h>
#include <memory>
#include <aws/core/utils/memory/stl/AWSString.h>
using namespace Aws::Utils;
using namespace Aws::Auth;
using namespace Aws::Client;
using namespace Aws::DynamoDB;
using namespace Aws::DynamoDB::Model;
using namespace Aws::Http;
namespace Aws
{
namespace Example1
{
const char *ExampleTableName = "PlayerData";
const char *IdentityColumnName = "Identity";
const char *DeveloperCredentialsProfileName = "example1developer";
static const char* SchemaTag = "Schema";
enum class TableStatusWaitUntil
{
ACTIVE,
GONE
};
void WaitOnTableStatus(const std::unique_ptr<DynamoDBClient>& client, const char* tableName, TableStatusWaitUntil status)
{
DescribeTableRequest describeTableRequest;
describeTableRequest.SetTableName(tableName);
bool done = false;
while (!done)
{
DescribeTableOutcome outcome = client->DescribeTable(describeTableRequest);
switch(status)
{
case TableStatusWaitUntil::GONE:
if (outcome.IsSuccess())
{
AWS_LOGSTREAM_INFO(SchemaTag, ExampleTableName << " table not yet deleted, waiting.");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
else
{
assert(DynamoDBErrors::RESOURCE_NOT_FOUND == outcome.GetError().GetErrorType());
done = true;
}
break;
case TableStatusWaitUntil::ACTIVE:
assert(outcome.IsSuccess());
if (outcome.GetResult().GetTable().GetTableStatus() == TableStatus::ACTIVE)
{
done = true;
}
else
{
AWS_LOGSTREAM_INFO(SchemaTag, ExampleTableName << " table not yet active, waiting.");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
break;
default:
break;
}
}
}
bool DropPlayerDataTable(const std::unique_ptr<DynamoDBClient>& client)
{
AWS_LOGSTREAM_INFO(SchemaTag, "Dropping old table: " << ExampleTableName);
DeleteTableRequest deleteTableRequest;
deleteTableRequest.SetTableName(ExampleTableName);
DeleteTableOutcome deleteTableOutcome = client->DeleteTable(deleteTableRequest);
if (!deleteTableOutcome.IsSuccess())
{
AWSError<DynamoDBErrors> error = deleteTableOutcome.GetError();
AWS_LOGSTREAM_ERROR(SchemaTag, "Failed to drop old table: " << ExampleTableName << ": " << error.GetMessage() << "(" << error.GetExceptionName() << ")");
return false;
}
WaitOnTableStatus(client, ExampleTableName, TableStatusWaitUntil::GONE);
return true;
}
bool CreatePlayerDataTable(const std::unique_ptr<DynamoDBClient>& client)
{
AWS_LOGSTREAM_INFO(SchemaTag, "Creating " << ExampleTableName << " table");
// Build the create request
CreateTableRequest createTableRequest;
AttributeDefinition identityAttribute;
identityAttribute.SetAttributeName(IdentityColumnName);
identityAttribute.SetAttributeType(ScalarAttributeType::S);
KeySchemaElement identityKeySchemaElement;
identityKeySchemaElement.WithAttributeName(IdentityColumnName).WithKeyType(KeyType::HASH);
ProvisionedThroughput provisionedThroughput;
provisionedThroughput.SetReadCapacityUnits(5);
provisionedThroughput.SetWriteCapacityUnits(1);
createTableRequest.AddAttributeDefinitions(identityAttribute);
createTableRequest.AddKeySchema(identityKeySchemaElement);
createTableRequest.WithProvisionedThroughput(provisionedThroughput);
createTableRequest.WithTableName(ExampleTableName);
CreateTableOutcome createTableOutcome = client->CreateTable(createTableRequest);
assert(createTableOutcome.IsSuccess() || createTableOutcome.GetError().GetErrorType() == DynamoDBErrors::RESOURCE_IN_USE);
if(!createTableOutcome.IsSuccess())
{
AWSError<DynamoDBErrors> error = createTableOutcome.GetError();
AWS_LOGSTREAM_ERROR(SchemaTag, "Failed to create table: " << ExampleTableName << ": " << error.GetMessage().c_str() << "(" << error.GetExceptionName() << ")");
return false;
}
// Wait for table to become active
WaitOnTableStatus(client, ExampleTableName, TableStatusWaitUntil::ACTIVE);
return true;
}
bool InitializePlayerDataSchema(void)
{
AWS_LOGSTREAM_INFO(SchemaTag, "Checking schema");
ClientConfiguration config;
config.scheme = Scheme::HTTP;
config.connectTimeoutMs = 30000;
config.requestTimeoutMs = 30000;
auto credentialsProvider = std::make_shared<ProfileConfigFileAWSCredentialsProvider>(DeveloperCredentialsProfileName);
std::unique_ptr<DynamoDBClient> client(new DynamoDBClient(credentialsProvider, config));
DescribeTableRequest describeTableRequest;
describeTableRequest.SetTableName(ExampleTableName);
DescribeTableOutcome outcome = client->DescribeTable(describeTableRequest);
// does the table exist with the properties we expect it to have?
if(outcome.IsSuccess())
{
const TableDescription& description = outcome.GetResult().GetTable();
if(description.GetAttributeDefinitions().size() == 1)
{
const AttributeDefinition& attributeDef = description.GetAttributeDefinitions()[0];
if(attributeDef.GetAttributeName() == IdentityColumnName && attributeDef.GetAttributeType() == ScalarAttributeType::S)
{
if(description.GetTableStatus() == TableStatus::ACTIVE)
{
AWS_LOGSTREAM_INFO(SchemaTag, "Valid schema exists, skipping creation step");
return true;
}
}
}
}
AWS_LOGSTREAM_INFO(SchemaTag, "No valid schema found");
// Delete if a non-matching version of the table was found
if(outcome.IsSuccess())
{
if(!DropPlayerDataTable(client))
{
return false;
}
}
// Create the table
return CreatePlayerDataTable(client);
}
} // namespace Example1
} // namespace Aws