#!/usr/bin/env ruby # (c) Copyright 2014 Hewlett-Packard Development Company, L.P. # # 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. require_relative 'connection.rb' include Connection # create a security group for each blueprint module SecurityGroup def create_security_group(name) security_groups = Connection.network.security_groups.all({:name => name}) security_group = nil if security_groups.length >= 1 security_group = security_groups[0] else description = 'Security group for blueprint %s' % [name] security_group = Connection.network.security_groups.create( :name => name, :description => description ) end security_group end def delete_security_group(security_group_id) Connection.network.security_groups.get(security_group_id).destroy end def create_security_group_rule(security_group_id, protocol, port_min, port_max) begin rule = Connection.network.security_group_rules.create( :security_group_id => security_group_id, :direction => 'ingress', :protocol => protocol, :port_range_min => port_min, :port_range_max => port_max, :remote_ip_prefix => '0.0.0.0/0' ) rescue StandardError => e puts 'error creating the rule for port %s' % [port_min] end end def delete_security_group_rule(rule_id) Connection.network.security_group_rules.get(rule_id).destroy end def upload_keypair(name) begin home = File.expand_path('~') path = home + '/.hpcloud/' + name # this will create a new keypair key_pair = Connection.compute.key_pairs.create(:name => name) key_pair.write(path) rescue StandardError => e puts 'error uploading the keypair' end end end