# Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License 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. Namespaces: =: com.example.databases std: io.murano res: io.murano.resources sys: io.murano.system conf: io.murano.configuration Name: MySql Extends: - SqlDatabase Properties: instance: Contract: $.class(res:Instance).notNull() database: Contract: $.string() username: Contract: $.string() password: Contract: $.string() Methods: .init: Body: - $._environment: $.find(std:Environment).require() deploy: Body: - If: not $.getAttr(deployed, false) Then: - $._environment.reporter.report($this, 'Creating VM for MySql') - $securityGroupIngress: - ToPort: 3306 FromPort: 3306 IpProtocol: tcp External: true - $._environment.securityGroupManager.addGroupIngress($securityGroupIngress) - $.instance.deploy() # Deploy MySql - $._environment.reporter.report($this, 'Instance is created. Deploying MySql') - $file: sys:Resources.string('deployMySql.sh') - conf:Linux.runCommand($.instance.agent, $file) - $._environment.reporter.report($this, 'MySql application is installed.') - $.setAttr(deployed, true) - $._environment.reporter.report($this, 'Creating database and user.') - If: $.database != '' and $.database != null Then: - $.createDatabase($.database) - $._environment.reporter.report($this, format('Database {0} created.', $.database)) - If: $.username != '' and $.username != null Then: - $.createUser($.username, $.password) - If: $.database != '' and $.database != null Then: - $.assignUser($.username, $.database) - $._environment.reporter.report($this, format('User {0} created.', $.username)) - If: $.instance.assignFloatingIp Then: - $host: $.instance.floatingIpAddress Else: - $host: $.instance.ipAddresses.first() - $._environment.reporter.report($this, format('MySQL is available at {0}', $host)) createDatabase: Arguments: - database: Contract: $.string().notNull() Body: - $.deploy() - $._environment.reporter.report($this, format('Creating {0} database.', $database)) # Creating Database on MySQL - $replacement: "%DATABASE%": $database - $file: sys:Resources.string('createMySqlDatabase.sh').replace($replacement) - conf:Linux.runCommand($.instance.agent, $file) - $._environment.reporter.report($this, format('Database {0} created.', $database)) createUser: Arguments: - username: Contract: $.string().notNull() - password: Contract: $.string().notNull() Body: - $.deploy() - $._environment.reporter.report($this, format('Creating {0} user.', $username)) # Creating user on MySQL - $replacements: "%USER_NAME%": $username "%PASSWORD%": $password - $file: sys:Resources.string('createMySqlUser.sh').replace($replacements) - conf:Linux.runCommand($.instance.agent, $file) - $._environment.reporter.report($this, format('User {0} created', $username)) assignUser: Arguments: - username: Contract: $.string().notNull() - database: Contract: $.string().notNull() Body: - $.deploy() - $._environment.reporter.report($this, format('Assigning user {0} to database {1}.', $username, $database)) # Assigning user to MySQL database - $replacements: "%USER_NAME%": $username "%DB_NAME%": $database - $file: sys:Resources.string('assignMySqlUser.sh').replace($replacements) - conf:Linux.runCommand($.instance.agent, $file) - $._environment.reporter.report($this, format('User {0} assigned to database {1}.', $username, $database)) getConnectionString: Arguments: - username: Contract: $.string().notNull() - password: Contract: $.string().notNull() Body: - If: $.instance.assignFloatingIp Then: - $host: $.instance.floatingIpAddress Else: - $host: $.instance.ipAddresses.first() - Return: format('mysql://{0}@{1}:{2}', $username, $password, $host)