~ubuntu-branches/ubuntu/trusty/heat/trusty-security

« back to all changes in this revision

Viewing changes to heat/engine/resources/dbinstance.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-10-03 09:43:04 UTC
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20131003094304-zhhr2brapzlpvjmm
Tags: upstream-2013.2~rc1
ImportĀ upstreamĀ versionĀ 2013.2~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
#
4
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
 
#    not use this file except in compliance with the License. You may obtain
6
 
#    a copy of the License at
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
10
 
#    Unless required by applicable law or agreed to in writing, software
11
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 
#    License for the specific language governing permissions and limitations
14
 
#    under the License.
15
 
 
16
 
from heat.common import template_format
17
 
from heat.engine import stack_resource
18
 
from heat.openstack.common import log as logging
19
 
 
20
 
logger = logging.getLogger(__name__)
21
 
 
22
 
mysql_template = r'''
23
 
{
24
 
  "AWSTemplateFormatVersion": "2010-09-09",
25
 
  "Description": "Builtin RDS::DBInstance",
26
 
  "Parameters" : {
27
 
    "DBInstanceClass" : {
28
 
      "Type": "String"
29
 
    },
30
 
 
31
 
    "DBName" : {
32
 
      "Type": "String"
33
 
    },
34
 
 
35
 
    "MasterUsername" : {
36
 
      "Type": "String"
37
 
    },
38
 
 
39
 
    "MasterUserPassword" : {
40
 
      "Type": "String"
41
 
    },
42
 
 
43
 
    "AllocatedStorage" : {
44
 
      "Type": "String"
45
 
    },
46
 
 
47
 
    "DBSecurityGroups" : {
48
 
      "Type": "CommaDelimitedList",
49
 
      "Default": ""
50
 
    },
51
 
 
52
 
    "Port" : {
53
 
      "Type": "String"
54
 
    },
55
 
 
56
 
    "KeyName" : {
57
 
      "Type" : "String"
58
 
    }
59
 
  },
60
 
 
61
 
  "Mappings" : {
62
 
    "DBInstanceToInstance" : {
63
 
      "db.m1.small": {"Instance": "m1.small"},
64
 
      "db.m1.large": {"Instance": "m1.large"},
65
 
      "db.m1.xlarge": {"Instance": "m1.xlarge"},
66
 
      "db.m2.xlarge": {"Instance": "m2.xlarge"},
67
 
      "db.m2.2xlarge": {"Instance": "m2.2xlarge"},
68
 
      "db.m2.4xlarge": {"Instance": "m2.4xlarge"}
69
 
    }
70
 
  },
71
 
 
72
 
 
73
 
  "Resources": {
74
 
    "DatabaseInstance": {
75
 
      "Type": "AWS::EC2::Instance",
76
 
      "Metadata": {
77
 
        "AWS::CloudFormation::Init": {
78
 
          "config": {
79
 
            "packages": {
80
 
              "yum": {
81
 
                "mysql"        : [],
82
 
                "mysql-server" : []
83
 
              }
84
 
            },
85
 
            "services": {
86
 
              "systemd": {
87
 
                "mysqld"   : { "enabled" : "true", "ensureRunning" : "true" }
88
 
              }
89
 
            }
90
 
          }
91
 
        }
92
 
      },
93
 
      "Properties": {
94
 
        "ImageId": "F17-x86_64-cfntools",
95
 
        "InstanceType": { "Fn::FindInMap": [ "DBInstanceToInstance",
96
 
                                             { "Ref": "DBInstanceClass" },
97
 
                                             "Instance" ] },
98
 
        "KeyName": { "Ref": "KeyName" },
99
 
        "UserData": { "Fn::Base64": { "Fn::Join": ["", [
100
 
          "#!/bin/bash -v\n",
101
 
          "# Helper function\n",
102
 
          "function error_exit\n",
103
 
          "{\n",
104
 
          "  /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '",
105
 
          { "Ref" : "WaitHandle" }, "'\n",
106
 
          "  exit 1\n",
107
 
          "}\n",
108
 
 
109
 
          "/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackName" },
110
 
          " -r DatabaseInstance",
111
 
          " --region ", { "Ref" : "AWS::Region" },
112
 
          " || error_exit 'Failed to run cfn-init'\n",
113
 
          "# Setup MySQL root password and create a user\n",
114
 
          "mysqladmin -u root password '", {"Ref":"MasterUserPassword"},"'\n",
115
 
          "cat << EOF | mysql -u root --password='",
116
 
                                      { "Ref" : "MasterUserPassword" }, "'\n",
117
 
          "CREATE DATABASE ", { "Ref" : "DBName" }, ";\n",
118
 
          "GRANT ALL PRIVILEGES ON ", { "Ref" : "DBName" },
119
 
                    ".* TO \"", { "Ref" : "MasterUsername" }, "\"@\"%\"\n",
120
 
          "IDENTIFIED BY \"", { "Ref" : "MasterUserPassword" }, "\";\n",
121
 
          "FLUSH PRIVILEGES;\n",
122
 
          "EXIT\n",
123
 
          "EOF\n",
124
 
          "# Database setup completed, signal success\n",
125
 
          "/opt/aws/bin/cfn-signal -e 0 -r \"MySQL server setup complete\" '",
126
 
          { "Ref" : "WaitHandle" }, "'\n"
127
 
 
128
 
        ]]}}
129
 
      }
130
 
    },
131
 
 
132
 
    "WaitHandle" : {
133
 
      "Type" : "AWS::CloudFormation::WaitConditionHandle"
134
 
    },
135
 
 
136
 
    "WaitCondition" : {
137
 
      "Type" : "AWS::CloudFormation::WaitCondition",
138
 
      "DependsOn" : "DatabaseInstance",
139
 
      "Properties" : {
140
 
        "Handle" : {"Ref" : "WaitHandle"},
141
 
        "Timeout" : "600"
142
 
      }
143
 
    }
144
 
  },
145
 
 
146
 
  "Outputs": {
147
 
  }
148
 
}
149
 
'''
150
 
 
151
 
 
152
 
class DBInstance(stack_resource.StackResource):
153
 
 
154
 
    properties_schema = {
155
 
        'DBSnapshotIdentifier': {'Type': 'String',
156
 
                                 'Implemented': False},
157
 
        'AllocatedStorage': {'Type': 'String',
158
 
                             'Required': True},
159
 
        'AvailabilityZone': {'Type': 'String',
160
 
                             'Implemented': False},
161
 
        'BackupRetentionPeriod': {'Type': 'String',
162
 
                                  'Implemented': False},
163
 
        'DBInstanceClass': {'Type': 'String',
164
 
                            'Required': True},
165
 
        'DBName': {'Type': 'String',
166
 
                   'Required': False},
167
 
        'DBParameterGroupName': {'Type': 'String',
168
 
                                 'Implemented': False},
169
 
        'DBSecurityGroups': {'Type': 'List',
170
 
                             'Required': False, 'Default': []},
171
 
        'DBSubnetGroupName': {'Type': 'String',
172
 
                              'Implemented': False},
173
 
        'Engine': {'Type': 'String',
174
 
                   'AllowedValues': ['MySQL'],
175
 
                   'Required': True},
176
 
        'EngineVersion': {'Type': 'String',
177
 
                          'Implemented': False},
178
 
        'LicenseModel': {'Type': 'String',
179
 
                         'Implemented': False},
180
 
        'MasterUsername': {'Type': 'String',
181
 
                           'Required': True},
182
 
        'MasterUserPassword': {'Type': 'String',
183
 
                               'Required': True},
184
 
        'Port': {'Type': 'String',
185
 
                 'Default': '3306',
186
 
                 'Required': False},
187
 
        'PreferredBackupWindow': {'Type': 'String',
188
 
                                  'Implemented': False},
189
 
        'PreferredMaintenanceWindow': {'Type': 'String',
190
 
                                       'Implemented': False},
191
 
        'MultiAZ': {'Type': 'Boolean',
192
 
                    'Implemented': False},
193
 
    }
194
 
 
195
 
    # We only support a couple of the attributes right now
196
 
    attributes_schema = {
197
 
        "Endpoint.Address": "Connection endpoint for the database.",
198
 
        "Endpoint.Port": ("The port number on which the database accepts "
199
 
                          "connections.")
200
 
    }
201
 
 
202
 
    def _params(self):
203
 
        params = {
204
 
            'KeyName': {'Ref': 'KeyName'},
205
 
        }
206
 
 
207
 
        # Add the DBInstance parameters specified in the user's template
208
 
        # Ignore the not implemented ones
209
 
        for key, value in self.properties_schema.items():
210
 
            if value.get('Implemented', True) and key != 'Engine':
211
 
                # There is a mismatch between the properties "List" format
212
 
                # and the parameters "CommaDelimitedList" format, so we need
213
 
                # to translate lists into the expected comma-delimited form
214
 
                if isinstance(self.properties[key], list):
215
 
                    params[key] = ','.join(self.properties[key])
216
 
                else:
217
 
                    params[key] = self.properties[key]
218
 
        p = self.stack.resolve_static_data(params)
219
 
        return p
220
 
 
221
 
    def handle_create(self):
222
 
        templ = template_format.parse(mysql_template)
223
 
        return self.create_with_template(templ, self._params())
224
 
 
225
 
    def handle_delete(self):
226
 
        return self.delete_nested()
227
 
 
228
 
    def _resolve_attribute(self, name):
229
 
        '''
230
 
        We don't really support any of these yet.
231
 
        '''
232
 
        if name == 'Endpoint.Address':
233
 
            if self.nested() and 'DatabaseInstance' in self.nested().resources:
234
 
                return self.nested().resources['DatabaseInstance']._ipaddress()
235
 
            else:
236
 
                return '0.0.0.0'
237
 
        elif name == 'Endpoint.Port':
238
 
            return self.properties['Port']
239
 
 
240
 
 
241
 
def resource_mapping():
242
 
    return {
243
 
        'AWS::RDS::DBInstance': DBInstance,
244
 
    }