murano-apps/PostgreSQL/package/Classes/PostgreSql.yaml

142 lines
4.9 KiB
YAML

# 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:
=: io.murano.databases
std: io.murano
res: io.murano.resources
sys: io.murano.system
Name: PostgreSql
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 PostgreSql')
- $securityGroupIngress:
- ToPort: 5432
FromPort: 5432
IpProtocol: tcp
External: true
- $._environment.securityGroupManager.addGroupIngress($securityGroupIngress)
- $.instance.deploy()
- $resources: new(sys:Resources)
# Deploy PostgreSql
- $._environment.reporter.report($this, 'Instance is created. Deploying PostgreSql')
- $template: $resources.yaml('DeployPostgreSql.template')
- $.instance.agent.call($template, $resources)
- $._environment.reporter.report($this, 'PostgreSql 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[0]
- $._environment.reporter.report($this, format('PostgreSQL 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 PostgreSQL
- $resources: new(sys:Resources)
- $template: $resources.yaml('CreatePostgreSqlDatabase.template').bind(dict(
database => $database
))
- $.instance.agent.call($template, $resources)
- $._environment.reporter.report($this, 'Database is created')
createUser:
Arguments:
- username:
Contract: $.string().notNull()
- password:
Contract: $.string().notNull()
Body:
- $.deploy()
- $._environment.reporter.report($this, format('Creating {0} user.', $username))
# Creating Database on PostgreSQL
- $resources: new(sys:Resources)
- $template: $resources.yaml('CreatePostgreSqlUser.template').bind(dict(
username => $username,
password => $password
))
- $.instance.agent.call($template, $resources)
- $._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 PostgresQL database
- $resources: new(sys:Resources)
- $template: $resources.yaml('AssignUserPostgreSql.template').bind(dict(
username => $username,
database => $database
))
- $.instance.agent.call($template, $resources)
- $._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[0]
- Return: format('psql://{0}@{1}:{2}', $username, $password, $host)