-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Usage] Create VPC billing #7235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
589a337
Add VPC billing
e917c2a
Address stephankruggg reviews
7b94977
Address Daan reviews, changing script upgrade path
00ce971
Readd line
5b8122c
Fix end of files
b301b1d
Address Daan review.
fbf62cf
Change logger to log4j2 apis
BryanMLima 1706157
Fix leftovers logs
BryanMLima bb4659f
Add logs
BryanMLima 04159b0
Change add column to idempotent procedure
BryanMLima 518b79a
Fix logs
BryanMLima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
engine/schema/src/main/java/com/cloud/usage/UsageVpcVO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
29
engine/schema/src/main/java/com/cloud/usage/dao/UsageVpcDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
131
engine/schema/src/main/java/com/cloud/usage/dao/UsageVpcDaoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not for update?
There was a problem hiding this comment.
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.