|
49 | 49 | import org.apache.cloudstack.persistence.iactemplatesprofile.IacResourceTypeDao; |
50 | 50 | import org.apache.cloudstack.persistence.iactemplatesprofile.IacResourceTypeVO; |
51 | 51 | import org.apache.cloudstack.tosca.orchestrator.ToscaOrchestrator; |
52 | | -import org.apache.commons.collections4.CollectionUtils; |
53 | 52 | import org.apache.commons.lang3.ObjectUtils; |
54 | 53 |
|
55 | 54 | import javax.inject.Inject; |
@@ -117,46 +116,81 @@ private boolean doesUserHaveAccessToNodeTypeApis(Pair<String, String> nodeTypeAp |
117 | 116 | @Override |
118 | 117 | public IacTemplateResponse registerIacTemplate(RegisterIacTemplateCmd cmd) { |
119 | 118 | Account owner = accountService.getActiveAccountById(cmd.getEntityOwnerId()); |
120 | | - validateAccessToIacTemplateSharingEntities(owner, cmd); |
| 119 | + boolean isTemplateOwnerAdmin = accountService.isAdmin(owner.getId()); |
| 120 | + if (!isTemplateOwnerAdmin) { |
| 121 | + if (cmd.isRecursiveDomains()) { |
| 122 | + throw new InvalidParameterValueException(String.format("An IaC template owned by [%s] cannot be shared recursively across different domains.", owner.getAccountName())); |
| 123 | + } |
| 124 | + |
| 125 | + if (cmd.isTemplateShared()) { |
| 126 | + throw new InvalidParameterValueException(String.format("Account [%s] does not have permission to share IaC template with other entities.", owner.getAccountName())); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +// validateAccessToIacTemplateSharingEntities(owner, cmd); |
121 | 131 | toscaOrchestrator.parseServiceTemplate(cmd.getIacTemplateContent()); |
122 | 132 |
|
123 | 133 | IacTemplate iacTemplate = persistIacTemplate(cmd, owner); |
124 | 134 | if (iacTemplate == null) { |
125 | 135 | throw new CloudRuntimeException("Unable to register IaC template."); |
126 | 136 | } |
127 | | - return responseBuilder.createIacTemplateResponse(iacTemplate, true); |
| 137 | + return responseBuilder.createIacTemplateResponse(iacTemplate, false); |
128 | 138 | } |
129 | 139 |
|
130 | 140 | private IacTemplate persistIacTemplate(RegisterIacTemplateCmd cmd, Account owner) { |
131 | 141 | IacTemplateVO iacTemplate = new IacTemplateVO(cmd.getName(), cmd.getDescription(), cmd.getIacTemplateContent(), |
132 | 142 | cmd.isRecursiveDomains(), owner.getDomainId(), owner.getAccountId()); |
133 | 143 | return Transaction.execute((TransactionCallback<IacTemplate>) (status) -> { |
134 | 144 | IacTemplateVO persistedTemplate = iacTemplateDao.persist(iacTemplate); |
135 | | - List<IacTemplateDomainMapVO> domainMappings = cmd.getSharedDomainIds().stream() |
136 | | - .map(domainId -> iacTemplateDomainMapDao.persist(new IacTemplateDomainMapVO(persistedTemplate.getId(), domainId))) |
137 | | - .collect(Collectors.toList()); |
138 | | - List<IacTemplateAccountMapVO> accountMappings = cmd.getSharedAccountIds().stream() |
139 | | - .map(accountId -> iacTemplateAccountMapDao.persist(new IacTemplateAccountMapVO(persistedTemplate.getId(), accountId))) |
140 | | - .collect(Collectors.toList()); |
| 145 | + List<IacTemplateDomainMapVO> domainMappings = persistDomainMappings(cmd.getSharedDomainIds(), persistedTemplate.getId()); |
| 146 | + List<IacTemplateAccountMapVO> accountMappings = persistAccountMappings(cmd.getSharedAccountIds(), cmd.getSharedProjectIds(), persistedTemplate.getId(), owner); |
141 | 147 | persistedTemplate.setDomainMappings(domainMappings); |
142 | 148 | persistedTemplate.setAccountMappings(accountMappings); |
143 | 149 | return persistedTemplate; |
144 | 150 | }); |
145 | 151 | } |
146 | 152 |
|
147 | | - protected void validateAccessToIacTemplateSharingEntities(Account owner, RegisterIacTemplateCmd cmd) { |
148 | | - boolean isTemplateOwnerAdmin = accountService.isAdmin(owner.getId()); |
149 | | - if (!isTemplateOwnerAdmin) { |
150 | | - if (cmd.isRecursiveDomains()) { |
151 | | - throw new InvalidParameterValueException(String.format("An IaC template owned by [%s] cannot be shared recursively across different domains.", owner.getAccountName())); |
| 153 | + private List<IacTemplateAccountMapVO> persistAccountMappings(List<Long> sharedAccountIds, List<Long> sharedProjectIds, long iacTemplateId, Account iacTemplateOwner) { |
| 154 | + List<IacTemplateAccountMapVO> accountMappings = new ArrayList<>(); |
| 155 | + |
| 156 | + for (Long accountId : sharedAccountIds) { |
| 157 | + Account account = accountService.getActiveAccountById(accountId); |
| 158 | + if (account == null) { |
| 159 | + throw new InvalidParameterValueException(String.format("Unable to find account with ID [%s].", accountId)); |
152 | 160 | } |
| 161 | + accountService.checkAccess(iacTemplateOwner, null, false, account); |
| 162 | + IacTemplateAccountMapVO accountMapping = new IacTemplateAccountMapVO(iacTemplateId, accountId); |
| 163 | + iacTemplateAccountMapDao.persist(accountMapping); |
| 164 | + accountMappings.add(accountMapping); |
| 165 | + } |
153 | 166 |
|
154 | | - if (CollectionUtils.isNotEmpty(cmd.getSharedDomainIds()) || CollectionUtils.isNotEmpty(cmd.getSharedAccountIds()) |
155 | | - || CollectionUtils.isNotEmpty(cmd.getSharedProjectIds())) { |
156 | | - throw new InvalidParameterValueException(String.format("Account [%s] does not have permission to share IaC template with other entities.", owner.getAccountName())); |
| 167 | + for (Long projectId : sharedProjectIds) { |
| 168 | + Project project = projectManager.getProject(projectId); |
| 169 | + if (project == null) { |
| 170 | + throw new InvalidParameterValueException(String.format("Unable to find project with ID [%s].", projectId)); |
157 | 171 | } |
| 172 | + if (!projectManager.canAccessProjectAccount(iacTemplateOwner, project.getProjectAccountId())) { |
| 173 | + throw new InvalidParameterValueException(String.format("Account [%s] does not have permission to share IaC template with project [%s].", iacTemplateOwner.getAccountName(), project.getName())); |
| 174 | + } |
| 175 | + IacTemplateAccountMapVO accountMapping = new IacTemplateAccountMapVO(iacTemplateId, project.getProjectAccountId()); |
| 176 | + iacTemplateAccountMapDao.persist(accountMapping); |
| 177 | + accountMappings.add(accountMapping); |
158 | 178 | } |
159 | 179 |
|
| 180 | + return accountMappings; |
| 181 | + } |
| 182 | + |
| 183 | + private List<IacTemplateDomainMapVO> persistDomainMappings(List<Long> sharedDomainIds, long iacTemplateId) { |
| 184 | + return sharedDomainIds.stream() |
| 185 | + .map(domainId -> { |
| 186 | + IacTemplateDomainMapVO domainMapping = new IacTemplateDomainMapVO(iacTemplateId, domainId); |
| 187 | + iacTemplateDomainMapDao.persist(domainMapping); |
| 188 | + return domainMapping; |
| 189 | + }).collect(Collectors.toList()); |
| 190 | + } |
| 191 | + |
| 192 | + protected void validateAccessToIacTemplateSharingEntities(Account owner, RegisterIacTemplateCmd cmd) { |
| 193 | + |
160 | 194 | cmd.getSharedDomainIds().forEach(domainId -> { |
161 | 195 | Domain domain = domainManager.getDomain(domainId); |
162 | 196 | if (domain == null) { |
@@ -210,7 +244,7 @@ public List<Class<?>> getCommands() { |
210 | 244 | if (!NimbleServiceEnabled.value()) { |
211 | 245 | return commands; |
212 | 246 | } |
213 | | - return List.of(ListIacResourceTypesCmd.class, DeployIacTemplateCmd.class); |
| 247 | + return List.of(ListIacResourceTypesCmd.class, RegisterIacTemplateCmd.class, DeployIacTemplateCmd.class); |
214 | 248 | } |
215 | 249 |
|
216 | 250 | @Override |
|
0 commit comments