304-305 DB upgrade: upgrade for VPC and resourceTags feature

This commit is contained in:
Alena Prokharchyk 2012-07-19 16:30:23 -07:00
parent ba0c448174
commit f243765758
5 changed files with 271 additions and 2 deletions

View File

@ -72,7 +72,6 @@ import com.cloud.network.PhysicalNetworkServiceProvider;
import com.cloud.network.PublicIpAddress;
import com.cloud.network.Site2SiteCustomerGatewayVO;
import com.cloud.network.Site2SiteVpnConnection;
import com.cloud.network.Site2SiteVpnGateway;
import com.cloud.network.Site2SiteVpnGatewayVO;
import com.cloud.network.VirtualRouterProvider;
import com.cloud.network.VirtualRouterProvider.VirtualRouterProviderType;

View File

@ -14,6 +14,9 @@ package com.cloud.upgrade.dao;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;
@ -50,7 +53,8 @@ public class Upgrade304to305 extends Upgrade30xBase implements DbUpgrade {
@Override
public void performDataMigration(Connection conn) {
addVpcProvider(conn);
updateRouterNetworkRef(conn);
}
@Override
@ -62,4 +66,109 @@ public class Upgrade304to305 extends Upgrade30xBase implements DbUpgrade {
return new File[] { new File(script) };
}
private void addVpcProvider(Connection conn){
//Encrypt config params and change category to Hidden
s_logger.debug("Adding vpc provider to all physical networks in the system");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("SELECT id FROM `cloud`.`physical_network` WHERE removed is NULL");
rs = pstmt.executeQuery();
while (rs.next()) {
Long pNtwkId = rs.getLong(1);
//insert provider
pstmt = conn.prepareStatement("INSERT INTO `cloud`.`physical_network_service_providers` " +
"(`physical_network_id`, `provider_name`, `state`, `vpn_service_provided`, `dhcp_service_provided`, " +
"`dns_service_provided`, `gateway_service_provided`, `firewall_service_provided`, `source_nat_service_provided`," +
" `load_balance_service_provided`, `static_nat_service_provided`, `port_forwarding_service_provided`," +
" `user_data_service_provided`, `security_group_service_provided`) " +
"VALUES (?, 'VpcVirtualRouter', 'Enabled', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)");
pstmt.setLong(1, pNtwkId);
pstmt.executeUpdate();
//get provider id
pstmt = conn.prepareStatement("SELECT id FROM `cloud`.`physical_network_service_providers` " +
"WHERE physical_network_id=? and provider_name='VpcVirtualRouter'");
pstmt.setLong(1, pNtwkId);
ResultSet rs1 = pstmt.executeQuery();
rs1.next();
long providerId = rs1.getLong(1);
//insert VR element
pstmt = conn.prepareStatement("INSERT INTO `cloud`.`virtual_router_providers` (`nsp_id`, `type`, `enabled`) " +
"VALUES (?, 'VPCVirtualRouter', 1)");
pstmt.setLong(1, providerId);
pstmt.executeUpdate();
s_logger.debug("Added VPC Virtual router provider for physical network id=" + pNtwkId);
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable add VPC physical network service provider ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
s_logger.debug("Done adding VPC physical network service providers to all physical networks");
}
private void updateRouterNetworkRef(Connection conn){
//Encrypt config params and change category to Hidden
s_logger.debug("Updating router network ref");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("SELECT d.id, d.network_id FROM `cloud`.`domain_router` d, `cloud`.`vm_instance` v " +
"WHERE d.id=v.id AND v.removed is NULL");
rs = pstmt.executeQuery();
while (rs.next()) {
Long routerId = rs.getLong(1);
Long networkId = rs.getLong(2);
//get the network type
pstmt = conn.prepareStatement("SELECT guest_type from `cloud`.`networks` where id=?");
pstmt.setLong(1, networkId);
ResultSet rs1 = pstmt.executeQuery();
rs1.next();
String networkType = rs1.getString(1);
//insert the reference
pstmt = conn.prepareStatement("INSERT INTO `cloud`.`router_network_ref` (router_id, network_id, guest_type) " +
"VALUES (?, ?, ?)");
pstmt.setLong(1, routerId);
pstmt.setLong(2, networkId);
pstmt.setString(3, networkType);
pstmt.executeUpdate();
s_logger.debug("Added reference for router id=" + routerId + " and network id=" + networkId);
}
} catch (SQLException e) {
throw new CloudRuntimeException("Failed to update the router/network reference ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
s_logger.debug("Done updating router/network references");
}
}

View File

@ -29,6 +29,7 @@ public class Upgarde304to305Test extends TestCase{
@Override
@Before
public void setUp() throws Exception {
//DbTestUtils.executeScript("PreviousDatabaseSchema/304/cloud_304.sql", false, true);
}
@Override

View File

@ -14,3 +14,5 @@
#Schema cleanup from 3.0.4 to 3.0.5;
ALTER TABLE `cloud`.`domain_router` DROP COLUMN network_id;

View File

@ -12,3 +12,161 @@
#Schema upgrade from 3.0.4 to 3.0.5;
CREATE TABLE `cloud`.`resource_tags` (
`id` bigint unsigned NOT NULL auto_increment COMMENT 'id',
`uuid` varchar(40),
`key` varchar(255),
`value` varchar(255),
`resource_id` bigint unsigned NOT NULL,
`resource_uuid` varchar(40),
`resource_type` varchar(255),
`customer` varchar(255),
`domain_id` bigint unsigned NOT NULL COMMENT 'foreign key to domain id',
`account_id` bigint unsigned NOT NULL COMMENT 'owner of this network',
PRIMARY KEY (`id`),
CONSTRAINT `fk_tags__account_id` FOREIGN KEY(`account_id`) REFERENCES `account`(`id`),
CONSTRAINT `fk_tags__domain_id` FOREIGN KEY(`domain_id`) REFERENCES `domain`(`id`),
UNIQUE `i_tags__resource_id__resource_type__key`(`resource_id`, `resource_type`, `key`),
CONSTRAINT `uc_resource_tags__uuid` UNIQUE (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`vpc_offerings` (
`id` bigint unsigned NOT NULL auto_increment COMMENT 'id',
`uuid` varchar(40) NOT NULL,
`unique_name` varchar(64) UNIQUE COMMENT 'unique name of the vpc offering',
`name` varchar(255) COMMENT 'vpc name',
`display_text` varchar(255) COMMENT 'display text',
`state` char(32) COMMENT 'state of the vpc offering that has Disabled value by default',
`default` int(1) unsigned NOT NULL DEFAULT 0 COMMENT '1 if vpc offering is default',
`removed` datetime COMMENT 'date removed if not null',
`created` datetime NOT NULL COMMENT 'date created',
`service_offering_id` bigint unsigned COMMENT 'service offering id that virtual router is tied to',
PRIMARY KEY (`id`),
INDEX `i_vpc__removed`(`removed`),
CONSTRAINT `fk_vpc_offerings__service_offering_id` FOREIGN KEY `fk_vpc_offerings__service_offering_id` (`service_offering_id`) REFERENCES `service_offering`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`vpc_offering_service_map` (
`id` bigint unsigned NOT NULL auto_increment,
`vpc_offering_id` bigint unsigned NOT NULL COMMENT 'vpc_offering_id',
`service` varchar(255) NOT NULL COMMENT 'service',
`provider` varchar(255) COMMENT 'service provider',
`created` datetime COMMENT 'date created',
PRIMARY KEY (`id`),
CONSTRAINT `fk_vpc_offering_service_map__vpc_offering_id` FOREIGN KEY(`vpc_offering_id`) REFERENCES `vpc_offerings`(`id`) ON DELETE CASCADE,
UNIQUE (`vpc_offering_id`, `service`, `provider`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`vpc` (
`id` bigint unsigned NOT NULL auto_increment COMMENT 'id',
`uuid` varchar(40) NOT NULL,
`name` varchar(255) COMMENT 'vpc name',
`display_text` varchar(255) COMMENT 'vpc display text',
`cidr` varchar(18) COMMENT 'vpc cidr',
`vpc_offering_id` bigint unsigned NOT NULL COMMENT 'vpc offering id that this vpc is created from',
`zone_id` bigint unsigned NOT NULL COMMENT 'the id of the zone this Vpc belongs to',
`state` varchar(32) NOT NULL COMMENT 'state of the VP (can be Enabled and Disabled)',
`domain_id` bigint unsigned NOT NULL COMMENT 'domain the vpc belongs to',
`account_id` bigint unsigned NOT NULL COMMENT 'owner of this vpc',
`network_domain` varchar(255) COMMENT 'network domain',
`removed` datetime COMMENT 'date removed if not null',
`created` datetime NOT NULL COMMENT 'date created',
`restart_required` int(1) unsigned NOT NULL DEFAULT 0 COMMENT '1 if restart is required for the VPC',
PRIMARY KEY (`id`),
INDEX `i_vpc__removed`(`removed`),
CONSTRAINT `fk_vpc__zone_id` FOREIGN KEY `fk_vpc__zone_id` (`zone_id`) REFERENCES `data_center` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_vpc__vpc_offering_id` FOREIGN KEY (`vpc_offering_id`) REFERENCES `vpc_offerings`(`id`),
CONSTRAINT `fk_vpc__account_id` FOREIGN KEY `fk_vpc__account_id` (`account_id`) REFERENCES `account`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_vpc__domain_id` FOREIGN KEY `fk_vpc__domain_id` (`domain_id`) REFERENCES `domain`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`router_network_ref` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
`router_id` bigint unsigned NOT NULL COMMENT 'router id',
`network_id` bigint unsigned NOT NULL COMMENT 'network id',
`guest_type` char(32) COMMENT 'type of guest network that can be shared or isolated',
PRIMARY KEY (`id`),
CONSTRAINT `fk_router_network_ref__networks_id` FOREIGN KEY (`network_id`) REFERENCES `networks`(`id`) ON DELETE CASCADE,
UNIQUE `i_router_network_ref__router_id__network_id`(`router_id`, `network_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`vpc_gateways` (
`id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT COMMENT 'id',
`uuid` varchar(40),
`ip4_address` char(40) COMMENT 'ip4 address of the gateway',
`netmask` varchar(15) COMMENT 'netmask of the gateway',
`gateway` varchar(15) COMMENT 'gateway',
`vlan_tag` varchar(255),
`type` varchar(32) COMMENT 'type of gateway; can be Public/Private/Vpn',
`network_id` bigint unsigned NOT NULL COMMENT 'network id vpc gateway belongs to',
`vpc_id` bigint unsigned NOT NULL COMMENT 'id of the vpc the gateway belongs to',
`zone_id` bigint unsigned NOT NULL COMMENT 'id of the zone the gateway belongs to',
`created` datetime COMMENT 'date created',
`account_id` bigint unsigned NOT NULL COMMENT 'owner id',
`domain_id` bigint unsigned NOT NULL COMMENT 'domain id',
`state` varchar(32) NOT NULL COMMENT 'what state the vpc gateway in',
`removed` datetime COMMENT 'date removed if not null',
PRIMARY KEY (`id`),
CONSTRAINT `fk_vpc_gateways__network_id` FOREIGN KEY `fk_vpc_gateways__network_id`(`network_id`) REFERENCES `networks`(`id`),
CONSTRAINT `fk_vpc_gateways__vpc_id` FOREIGN KEY `fk_vpc_gateways__vpc_id`(`vpc_id`) REFERENCES `vpc`(`id`),
CONSTRAINT `fk_vpc_gateways__zone_id` FOREIGN KEY `fk_vpc_gateways__zone_id`(`zone_id`) REFERENCES `data_center`(`id`),
CONSTRAINT `fk_vpc_gateways__account_id` FOREIGN KEY(`account_id`) REFERENCES `account`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_vpc_gateways__domain_id` FOREIGN KEY(`domain_id`) REFERENCES `domain`(`id`) ON DELETE CASCADE,
CONSTRAINT `uc_vpc_gateways__uuid` UNIQUE (`uuid`),
INDEX `i_vpc_gateways__removed`(`removed`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`private_ip_address` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`ip_address` char(40) NOT NULL COMMENT 'ip address',
`network_id` bigint unsigned NOT NULL COMMENT 'id of the network ip belongs to',
`reservation_id` char(40) COMMENT 'reservation id',
`mac_address` varchar(17) COMMENT 'mac address',
`vpc_id` bigint unsigned COMMENT 'vpc this ip belongs to',
`taken` datetime COMMENT 'Date taken',
PRIMARY KEY (`id`),
CONSTRAINT `fk_private_ip_address__vpc_id` FOREIGN KEY `fk_private_ip_address__vpc_id`(`vpc_id`) REFERENCES `vpc`(`id`),
CONSTRAINT `fk_private_ip_address__network_id` FOREIGN KEY (`network_id`) REFERENCES `networks` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`static_routes` (
`id` bigint unsigned NOT NULL auto_increment COMMENT 'id',
`uuid` varchar(40),
`vpc_gateway_id` bigint unsigned COMMENT 'id of the corresponding ip address',
`cidr` varchar(18) COMMENT 'cidr for the static route',
`state` char(32) NOT NULL COMMENT 'current state of this rule',
`vpc_id` bigint unsigned COMMENT 'vpc the firewall rule is associated with',
`account_id` bigint unsigned NOT NULL COMMENT 'owner id',
`domain_id` bigint unsigned NOT NULL COMMENT 'domain id',
`created` datetime COMMENT 'Date created',
PRIMARY KEY (`id`),
CONSTRAINT `fk_static_routes__vpc_gateway_id` FOREIGN KEY(`vpc_gateway_id`) REFERENCES `vpc_gateways`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_static_routes__vpc_id` FOREIGN KEY (`vpc_id`) REFERENCES `vpc`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_static_routes__account_id` FOREIGN KEY(`account_id`) REFERENCES `account`(`id`) ON DELETE CASCADE,
CONSTRAINT `fk_static_routes__domain_id` FOREIGN KEY(`domain_id`) REFERENCES `domain`(`id`) ON DELETE CASCADE,
CONSTRAINT `uc_static_routes__uuid` UNIQUE (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `cloud`.`networks` ADD COLUMN `vpc_id` bigint unsigned COMMENT 'vpc this network belongs to';
ALTER TABLE `cloud`.`networks`ADD CONSTRAINT `fk_networks__vpc_id` FOREIGN KEY(`vpc_id`) REFERENCES `vpc`(`id`);
ALTER TABLE `cloud`.`firewall_rules` ADD COLUMN `vpc_id` bigint unsigned COMMENT 'vpc the firewall rule is associated with';
ALTER TABLE `cloud`.`firewall_rules` ADD COLUMN `traffic_type` char(32) COMMENT 'the type of the rule, can be Ingress or Egress';
ALTER TABLE `cloud`.`firewall_rules` MODIFY `ip_address_id` bigint unsigned COMMENT 'id of the corresponding ip address';
ALTER TABLE `cloud`.`firewall_rules` ADD CONSTRAINT `fk_firewall_rules__vpc_id` FOREIGN KEY (`vpc_id`) REFERENCES `vpc`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`user_ip_address` ADD COLUMN `vpc_id` bigint unsigned COMMENT 'vpc the ip address is associated with';
ALTER TABLE `cloud`.`user_ip_address` ADD CONSTRAINT `fk_user_ip_address__vpc_id` FOREIGN KEY (`vpc_id`) REFERENCES `vpc`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`domain_router` ADD COLUMN `vpc_id` bigint unsigned COMMENT 'correlated virtual router vpc ID';
ALTER TABLE `cloud`.`domain_router` ADD CONSTRAINT `fk_domain_router__vpc_id` FOREIGN KEY `fk_domain_router__vpc_id`(`vpc_id`) REFERENCES `vpc`(`id`);
ALTER TABLE `cloud`.`physical_network_service_providers` ADD COLUMN `networkacl_service_provided` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT 'Is Network ACL service provided';
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Advanced', 'DEFAULT', 'management-server', 'vpc.cleanup.interval', '3600', 'The interval (in seconds) between cleanup for Inactive VPCs');