Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1219,4 +1219,8 @@ public static Class getEntityClassForEvent(String eventName) {

return null;
}

public static boolean isVpcEvent(String eventType) {
return EventTypes.EVENT_VPC_CREATE.equals(eventType) || EventTypes.EVENT_VPC_DELETE.equals(eventType);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not for update?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DaanHoogland from my point of view, as the VPC update API only changes metadata such as name, MTU, etc., I see no need to handle the update event here.

}
}
2 changes: 2 additions & 0 deletions api/src/main/java/org/apache/cloudstack/usage/UsageTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class UsageTypes {
public static final int VM_SNAPSHOT_ON_PRIMARY = 27;
public static final int BACKUP = 28;
public static final int BUCKET = 29;
public static final int VPC = 31;

public static List<UsageTypeResponse> listUsageTypes() {
List<UsageTypeResponse> responseList = new ArrayList<UsageTypeResponse>();
Expand All @@ -72,6 +73,7 @@ public static List<UsageTypeResponse> listUsageTypes() {
responseList.add(new UsageTypeResponse(VM_SNAPSHOT_ON_PRIMARY, "VM Snapshot on primary storage usage"));
responseList.add(new UsageTypeResponse(BACKUP, "Backup storage usage"));
responseList.add(new UsageTypeResponse(BUCKET, "Bucket storage usage"));
responseList.add(new UsageTypeResponse(VPC, "VPC usage"));
return responseList;
}
}
130 changes: 130 additions & 0 deletions engine/schema/src/main/java/com/cloud/usage/UsageVpcVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package com.cloud.usage;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.cloudstack.api.InternalIdentity;

import java.util.Date;

@Entity
@Table(name = "usage_vpc")
public class UsageVpcVO implements InternalIdentity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;

@Column(name = "vpc_id")
private long vpcId;

@Column(name = "zone_id")
private long zoneId;

@Column(name = "account_id")
private long accountId;

@Column(name = "domain_id")
private long domainId;


@Column(name = "state")
private String state;

@Column(name = "created")
@Temporal(value = TemporalType.TIMESTAMP)
private Date created = null;

@Column(name = "removed")
@Temporal(value = TemporalType.TIMESTAMP)
private Date removed = null;

protected UsageVpcVO(){}

public UsageVpcVO(long id, long vpcId, long zoneId, long accountId, long domainId, String state, Date created, Date removed) {
this.id = id;
this.vpcId = vpcId;
this.zoneId = zoneId;
this.domainId = domainId;
this.accountId = accountId;
this.state = state;
this.created = created;
this.removed = removed;
}
public UsageVpcVO(long vpcId, long zoneId, long accountId, long domainId, String state, Date created, Date removed) {
this.vpcId = vpcId;
this.zoneId = zoneId;
this.domainId = domainId;
this.accountId = accountId;
this.state = state;
this.created = created;
this.removed = removed;
}

@Override
public long getId() {
return id;
}

public long getZoneId() {
return zoneId;
}

public long getAccountId() {
return accountId;
}

public long getDomainId() {
return domainId;
}

public long getVpcId() {
return vpcId;
}

public void setVpcId(long vpcId) {
this.vpcId = vpcId;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public Date getCreated() {
return created;
}

public Date getRemoved() {
return removed;
}

public void setRemoved(Date removed) {
this.removed = removed;
}
}
29 changes: 29 additions & 0 deletions engine/schema/src/main/java/com/cloud/usage/dao/UsageVpcDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package com.cloud.usage.dao;

import com.cloud.usage.UsageVpcVO;
import com.cloud.utils.db.GenericDao;

import java.util.Date;
import java.util.List;

public interface UsageVpcDao extends GenericDao<UsageVpcVO, Long> {
void update(UsageVpcVO usage);
void remove(long vpcId, Date removed);
List<UsageVpcVO> getUsageRecords(Long accountId, Date startDate, Date endDate);
}
131 changes: 131 additions & 0 deletions engine/schema/src/main/java/com/cloud/usage/dao/UsageVpcDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package com.cloud.usage.dao;

import com.cloud.network.vpc.Vpc;
import com.cloud.usage.UsageVpcVO;
import com.cloud.utils.DateUtil;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.TransactionLegacy;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Component
public class UsageVpcDaoImpl extends GenericDaoBase<UsageVpcVO, Long> implements UsageVpcDao {
private static final Logger LOGGER = Logger.getLogger(UsageVpcDaoImpl.class);
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT id, vpc_id, zone_id, account_id, domain_id, state, created, removed FROM usage_vpc WHERE " +
" account_id = ? AND ((removed IS NULL AND created <= ?) OR (created BETWEEN ? AND ?) OR (removed BETWEEN ? AND ?) " +
" OR ((created <= ?) AND (removed >= ?)))";

@Override
public void update(UsageVpcVO usage) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
try {
SearchCriteria<UsageVpcVO> sc = this.createSearchCriteria();
sc.addAnd("vpcId", SearchCriteria.Op.EQ, usage.getVpcId());
sc.addAnd("created", SearchCriteria.Op.EQ, usage.getCreated());
UsageVpcVO vo = findOneBy(sc);
if (vo != null) {
vo.setRemoved(usage.getRemoved());
update(vo.getId(), vo);
}
} catch (final Exception e) {
LOGGER.error(String.format("Error updating usage of VPC due to [%s].", e.getMessage()), e);
txn.rollback();
} finally {
txn.close();
}
}

@Override
public void remove(long vpcId, Date removed) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
try {
SearchCriteria<UsageVpcVO> sc = this.createSearchCriteria();
sc.addAnd("vpcId", SearchCriteria.Op.EQ, vpcId);
sc.addAnd("removed", SearchCriteria.Op.NULL);
UsageVpcVO vo = findOneBy(sc);
if (vo != null) {
vo.setRemoved(removed);
vo.setState(Vpc.State.Inactive.name());
update(vo.getId(), vo);
}
} catch (final Exception e) {
txn.rollback();
LOGGER.error(String.format("Error updating usage of VPC due to [%s].", e.getMessage()), e);
} finally {
txn.close();
}
}

@Override
public List<UsageVpcVO> getUsageRecords(Long accountId, Date startDate, Date endDate) {
List<UsageVpcVO> usageRecords = new ArrayList<>();
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
PreparedStatement pstmt;
try {
int i = 1;
pstmt = txn.prepareAutoCloseStatement(GET_USAGE_RECORDS_BY_ACCOUNT);
pstmt.setLong(i++, accountId);

pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));

ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
long id = rs.getLong(1);
long vpcId = rs.getLong(2);
long zoneId = rs.getLong(3);
long acctId = rs.getLong(4);
long domId = rs.getLong(5);
String stateTS = rs.getString(6);
Date createdDate = null;
Date removedDate = null;
String createdTS = rs.getString(7);
String removedTS = rs.getString(8);

if (createdTS != null) {
createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
}
if (removedTS != null) {
removedDate = DateUtil.parseDateString(s_gmtTimeZone, removedTS);
}
usageRecords.add(new UsageVpcVO(id, vpcId, zoneId, acctId, domId, stateTS, createdDate, removedDate));
}
} catch (Exception e) {
txn.rollback();
LOGGER.warn("Error getting VPC usage records", e);
} finally {
txn.close();
}

return usageRecords;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<bean id="vMInstanceDaoImpl" class="com.cloud.vm.dao.VMInstanceDaoImpl" />
<bean id="vMSnapshotDaoImpl" class="com.cloud.vm.snapshot.dao.VMSnapshotDaoImpl" />
<bean id="VmTemplateDaoImpl" class="org.apache.cloudstack.quota.dao.VmTemplateDaoImpl" />
<bean id="VpcDaoImpl" class="org.apache.cloudstack.quota.dao.VpcDaoImpl" />
<bean id="volumeDaoImpl" class="com.cloud.storage.dao.VolumeDaoImpl" />
<bean id="backupOfferingDaoImpl" class="org.apache.cloudstack.backup.dao.BackupOfferingDaoImpl" />
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
<bean id="usageStorageDaoImpl" class="com.cloud.usage.dao.UsageStorageDaoImpl" />
<bean id="usageVMInstanceDaoImpl" class="com.cloud.usage.dao.UsageVMInstanceDaoImpl" />
<bean id="usageVPNUserDaoImpl" class="com.cloud.usage.dao.UsageVPNUserDaoImpl" />
<bean id="usageVpcDaoImpl" class="com.cloud.usage.dao.UsageVpcDaoImpl" />
<bean id="usageVolumeDaoImpl" class="com.cloud.usage.dao.UsageVolumeDaoImpl" />
<bean id="usageVmDiskDaoImpl" class="com.cloud.usage.dao.UsageVmDiskDaoImpl" />
<bean id="usageBackupDaoImpl" class="com.cloud.usage.dao.UsageBackupDaoImpl" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ UPDATE cloud_usage.quota_tariff
SET usage_unit = 'IOPS', updated_on = NOW()
WHERE effective_on = '2010-05-04 00:00:00'
AND name IN ('VM_DISK_IO_READ', 'VM_DISK_IO_WRITE');

-- PR #7235 - [Usage] Create VPC billing
CREATE TABLE IF NOT EXISTS `cloud_usage`.`usage_vpc` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`vpc_id` bigint(20) unsigned NOT NULL,
`zone_id` bigint(20) unsigned NOT NULL,
`account_id` bigint(20) unsigned NOT NULL,
`domain_id` bigint(20) unsigned NOT NULL,
`state` varchar(100) DEFAULT NULL,
`created` datetime NOT NULL,
`removed` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARSET=utf8;

CALL `cloud_usage`.`IDEMPOTENT_ADD_COLUMN`('cloud_usage.cloud_usage', 'state', 'VARCHAR(100) DEFAULT NULL');
Loading