Skip to content
Snippets Groups Projects
Commit 0eb4e160 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3068037 by Xiaohua Guan, yas, Masami: Manage VPC subnets

parent 88e5459d
No related branches found
No related tags found
No related merge requests found
Showing
with 1738 additions and 3 deletions
......@@ -1161,6 +1161,32 @@ function aws_cloud_update_8166() {
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Add resource Subnet.
*
* Add view aws_cloud_subnet and entity type aws_cloud_subnet
* and aws_cloud_vpc_delete_action.
*/
function aws_cloud_update_8167() {
$files = [
'views.view.aws_cloud_subnet.yml',
'system.action.aws_cloud_subnet_delete_action.yml',
];
cloud_update_yml_definitions($files, 'aws_cloud');
// Add entity type aws_cloud_subnet.
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = \Drupal::entityTypeManager()->getDefinition('aws_cloud_subnet');
$definition_update_manager->installEntityType($entity_type);
// Update subnets.
foreach ($entities as $entity) {
/* @var \Drupal\cloud\Entity\CloudConfig $entity */
$aws_ec2_service->setCloudContext($entity->getCloudContext());
$aws_ec2_service->updateSubnets();
}
}
/**
* Helper function to add fields to the entity type.
*
......
......@@ -437,6 +437,45 @@ entity.aws_cloud_vpc.delete:
appears_on:
- entity.aws_cloud_vpc.canonical
####################
# AWS Cloud Subnet
####################
entity.aws_cloud_subnet.add_form:
route_name: entity.aws_cloud_subnet.add_form
title: 'Add AWS Cloud Subnet'
appears_on:
- view.aws_cloud_subnet.list
entity.aws_cloud_subnet.collection:
route_name: view.aws_cloud_subnet.list
title: 'List AWS Cloud Subnets'
appears_on:
- entity.aws_cloud_subnet.add_form
- entity.aws_cloud_subnet.edit_form
- entity.aws_cloud_subnet.delete_form
- entity.aws_cloud_subnet.canonical
entity.aws_cloud_subnet.refresh:
route_name: entity.aws_cloud_subnet.list_update
title: 'Refresh'
appears_on:
- view.aws_cloud_subnet.list
entity.aws_cloud_subnet.edit:
route_name: entity.aws_cloud_subnet.edit_form
title: 'Edit'
appears_on:
- entity.aws_cloud_subnet.canonical
entity.aws_cloud_subnet.delete:
route_name: entity.aws_cloud_subnet.delete_form
title: 'Delete'
appears_on:
- entity.aws_cloud_subnet.canonical
aws_cloud.instance_type_prices:
route_name: aws_cloud.instance_type_prices
title: 'Instance Type Prices'
......
......@@ -90,6 +90,12 @@ aws_cloud.local_tasks.vpc:
base_route: 'aws_cloud.local_tasks.cloud_context'
weight: 10
aws_cloud.local_tasks.subnet:
title: 'Subnets'
route_name: 'view.aws_cloud_subnet.list'
base_route: 'aws_cloud.local_tasks.cloud_context'
weight: 10
aws_cloud.local_tasks.instance_type_price:
title: 'Instance Type Prices'
route_name: 'aws_cloud.instance_type_prices'
......
......@@ -629,6 +629,7 @@ function aws_cloud_update_ec2_resources(
$aws_ec2_service->updateSnapshots();
$aws_ec2_service->updateVolumes();
$aws_ec2_service->updateVpcs();
$aws_ec2_service->updateSubnets();
$aws_ec2_service->updateCloudServerTemplates();
if ($for_cron) {
......
......@@ -240,6 +240,34 @@ delete own aws cloud vpc:
title: 'Delete own AWS Cloud vpc'
description: 'Allow users to delete own Amazon vpc'
#####################
# Amazon EBS Subnet
#####################
add aws cloud subnet:
title: 'Add AWS Cloud subnet'
description: 'Allow users to add Amazon subnet'
list aws cloud subnet:
title: 'List AWS Cloud subnet'
description: 'Allow users to list Amazon subnet'
view any aws cloud subnet:
title: 'View any AWS Cloud subnet'
description: 'Allow users to view any Amazon subnet'
edit any aws cloud subnet:
title: 'Edit any AWS Cloud subnet'
description: 'Allow users to edit any Amazon subnet'
delete any aws cloud subnet:
title: 'Delete any AWS Cloud subnet'
description: 'Allow users to delete any Amazon subnet'
view own aws cloud subnet:
title: 'View own AWS Cloud subnet'
description: 'Allow users to view own Amazon subnet'
edit own aws cloud subnet:
title: 'Edit own AWS Cloud subnet'
description: 'Allow users to edit own Amazon subnet'
delete own aws cloud subnet:
title: 'Delete own AWS Cloud subnet'
description: 'Allow users to delete own Amazon subnet'
#############################
# Amazon EC2 Instance Pricing
#############################
......
......@@ -97,6 +97,17 @@ entity.aws_cloud_vpc.list_update:
options:
perm: 'edit any aws cloud vpc+edit own aws cloud vpc'
entity.aws_cloud_subnet.list_update:
path: '/clouds/aws_cloud/{cloud_context}/subnet/update'
defaults:
_controller: '\Drupal\aws_cloud\Controller\Vpc\ApiController::updateSubnetList'
requirements:
# Use custom access that will check for cloud_context and the desired permission.
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'edit any aws cloud subnet+edit own aws cloud subnet'
entity.cloud_server_template.list_update:
path: '/clouds/design/server_template/list/{cloud_context}/update'
defaults:
......@@ -660,7 +671,7 @@ entity.aws_cloud_snapshot.edit_form:
_entity_access: 'aws_cloud_snapshot.edit'
entity.aws_cloud_snapshot.edit_form.edit:
path: '/clouds/aws_cloud/{cloud_context}/snapshot/edit/{aws_cloud_snapshot}/edit'
path: '/clouds/aws_cloud/{cloud_context}/snapshot/{aws_cloud_snapshot}/edit'
defaults:
_entity_form: 'aws_cloud_snapshot.edit'
_title: 'Edit AWS Cloud Snapshot'
......@@ -706,7 +717,7 @@ entity.aws_cloud_vpc.edit_form:
_entity_access: 'aws_cloud_vpc.edit'
entity.aws_cloud_vpc.edit_form.edit:
path: '/clouds/aws_cloud/{cloud_context}/vpc/edit/{aws_cloud_vpc}/edit'
path: '/clouds/aws_cloud/{cloud_context}/vpc/{aws_cloud_vpc}/edit'
defaults:
_entity_form: 'aws_cloud_vpc.edit'
_title: 'Edit AWS Cloud VPC'
......@@ -722,6 +733,52 @@ entity.aws_cloud_vpc.delete_form:
_entity_access: 'aws_cloud_vpc.delete'
# AWS Cloud Subnets Routes
entity.aws_cloud_subnet.canonical:
path: '/clouds/aws_cloud/{cloud_context}/subnet/{aws_cloud_subnet}'
defaults:
_entity_view: 'aws_cloud_subnet'
_title: 'AWS Cloud Subnet'
requirements:
_entity_access: 'aws_cloud_subnet.view'
entity.aws_cloud_subnet.add_form:
path: '/clouds/aws_cloud/{cloud_context}/subnet/add'
defaults:
_entity_form: 'aws_cloud_subnet.add'
_title: 'Add AWS Cloud Subnet'
requirements:
# Use custom access that will check for cloud_context and the desired permission.
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'add aws cloud subnet'
entity.aws_cloud_subnet.edit_form:
path: '/clouds/aws_cloud/{cloud_context}/subnet/{aws_cloud_subnet}/edit'
defaults:
_entity_form: 'aws_cloud_subnet.edit'
_title: 'Edit AWS Cloud Subnet'
requirements:
_entity_access: 'aws_cloud_subnet.edit'
entity.aws_cloud_subnet.edit_form.edit:
path: '/clouds/aws_cloud/{cloud_context}/subnet/{aws_cloud_subnet}/edit'
defaults:
_entity_form: 'aws_cloud_subnet.edit'
_title: 'Edit AWS Cloud Subnet'
requirements:
_entity_access: 'aws_cloud_subnet.edit'
entity.aws_cloud_subnet.delete_form:
path: '/clouds/aws_cloud/{cloud_context}/subnet/{aws_cloud_subnet}/delete'
defaults:
_entity_form: 'aws_cloud_subnet.delete'
_title: 'Delete AWS Cloud Subnet'
requirements:
_entity_access: 'aws_cloud_subnet.delete'
entity.aws_cloud_instance.list_callback:
path: '/clouds/aws_cloud/{cloud_context}/instance/callback'
defaults:
......
langcode: en
status: true
dependencies:
module:
- aws_cloud
id: aws_cloud_subnet_delete_action
label: 'Delete subnet(s)'
type: aws_cloud_subnet
plugin: entity:delete_action:aws_cloud_subnet
configuration: { }
langcode: en
status: true
dependencies:
module:
- aws_cloud
- cloud
id: aws_cloud_subnet
label: 'AWS Cloud Subnet'
module: views
description: 'Replaces the ListBuilder for AWS Cloud Subnet entities'
tag: ''
base_table: aws_cloud_subnet
base_field: id
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: ViewsCloudContextAccess
options:
perm: 'list aws cloud subnet'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 50
offset: 0
id: 0
total_pages: null
expose:
items_per_page: true
items_per_page_label: 'Items per page'
items_per_page_options: '10, 15, 20, 25, 50, 100'
items_per_page_options_all: true
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ‹‹
next: ››
first: '« First'
last: 'Last »'
style:
type: table
options:
grouping: { }
row_class: ''
default_row_class: true
override: true
sticky: false
caption: ''
summary: ''
description: ''
columns:
subnet_bulk_form: subnet_bulk_form
name: name
subnet_id: subnet_id
vpc_id: vpc_id
cidr_block: cidr_block
state: state
created: created
operations: operations
info:
subnet_bulk_form:
align: ''
separator: ''
empty_column: false
responsive: ''
name:
sortable: true
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
subnet_id:
sortable: true
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
vpc_id:
sortable: true
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
cidr_block:
sortable: true
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
state:
sortable: true
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
created:
sortable: true
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
operations:
align: ''
separator: ''
empty_column: false
responsive: ''
default: name
empty_table: false
row:
type: fields
fields:
subnet_bulk_form:
id: subnet_bulk_form
table: aws_cloud_subnet
field: subnet_bulk_form
label: ''
exclude: false
alter:
alter_text: false
element_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
plugin_id: subnet_bulk_form
entity_type: aws_cloud_subnet
name:
id: name
table: aws_cloud_subnet
field: name
relationship: none
group_type: group
admin_label: ''
label: Name
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: true
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: aws_cloud_subnet
entity_field: name
plugin_id: field
subnet_id:
id: subnet_id
table: aws_cloud_subnet
field: subnet_id
relationship: none
group_type: group
admin_label: ''
label: 'Subnet ID'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: false
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: aws_cloud_subnet
entity_field: subnet_id
plugin_id: field
vpc_id:
id: vpc_id
table: aws_cloud_subnet
field: vpc_id
relationship: none
group_type: group
admin_label: ''
label: 'VPC ID'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: false
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: aws_cloud_subnet
entity_field: vpc_id
plugin_id: field
cidr_block:
id: cidr_block
table: aws_cloud_subnet
field: cidr_block
relationship: none
group_type: group
admin_label: ''
label: 'IPv4 CIDR'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: false
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: aws_cloud_subnet
entity_field: cidr_block
plugin_id: field
state:
id: state
table: aws_cloud_subnet
field: state
relationship: none
group_type: group
admin_label: ''
label: State
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: false
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: aws_cloud_subnet
entity_field: state
plugin_id: field
created:
id: created
table: aws_cloud_subnet
field: created
relationship: none
group_type: group
admin_label: ''
label: Created
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: timestamp
settings:
date_format: short
custom_date_format: ''
timezone: ''
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: aws_cloud_subnet
entity_field: created
plugin_id: field
operations:
id: operations
table: aws_cloud_subnet
field: operations
relationship: none
group_type: group
admin_label: ''
label: 'Operations links'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
destination: false
entity_type: null
entity_field: null
plugin_id: entity_operations
filters: { }
sorts: { }
title: 'AWS Cloud Subnets'
header: { }
footer: { }
empty: { }
relationships: { }
arguments:
cloud_context:
id: cloud_context
table: aws_cloud_subnet
field: cloud_context
relationship: none
group_type: group
admin_label: ''
default_action: default
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: raw
default_argument_options:
index: 2
use_alias: false
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
glossary: false
limit: 0
case: none
path_case: none
transform_dash: false
break_phrase: false
entity_type: aws_cloud_subnet
entity_field: cloud_context
plugin_id: string
display_extenders: { }
filter_groups:
operator: AND
groups: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- user.permissions
tags: { }
list:
display_plugin: page
id: list
display_title: List
position: 1
display_options:
display_extenders: { }
path: clouds/aws_cloud/%cloud_context/subnet
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- user.permissions
tags: { }
......@@ -26,10 +26,14 @@ views.field.vpc_bulk_form:
type: views_field_bulk_form
label: 'vpc bulk form'
views.field.subnet_bulk_form:
type: views_field_bulk_form
label: 'subnet bulk form'
views.field.network_interface_bulk_form:
type: views_field_bulk_form
label: 'network interface bulk form'
views.field.security_group_bulk_form:
type: views_field_bulk_form
label: 'security group bulk form'
\ No newline at end of file
label: 'security group bulk form'
......@@ -101,4 +101,24 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
]);
}
/**
* {@inheritdoc}
*/
public function updateSubnetList($cloud_context) {
$this->awsEc2Service->setCloudContext($cloud_context);
$updated = $this->awsEc2Service->updateSubnets();
if ($updated !== FALSE) {
$this->messenger->addMessage($this->t('Updated Subnets.'));
}
else {
$this->messenger->addError($this->t('Unable to update Subnets.'));
}
return $this->redirect('view.aws_cloud_subnet.list', [
'cloud_context' => $cloud_context,
]);
}
}
......@@ -18,4 +18,15 @@ interface ApiControllerInterface {
*/
public function updateVpcList($cloud_context);
/**
* Update all subnets in particular cloud region.
*
* @param string $cloud_context
* Cloud context string.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
public function updateSubnetList($cloud_context);
}
<?php
namespace Drupal\aws_cloud\Controller\Vpc;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
/**
* Access controller for the Subnet entity.
*
* @see \Drupal\aws_cloud\Entity\Vpc\Subnet.
*/
class SubnetAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if (!AccessResult::allowedIfHasPermission($account, 'view ' . $entity->getCloudContext())->isAllowed()) {
return AccessResult::neutral();
}
switch ($operation) {
case 'view':
if ($account->hasPermission('view any aws cloud subnet')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('view own aws cloud subnet') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'update':
case 'edit':
if ($account->hasPermission('edit any aws cloud subnet')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('edit own aws cloud subnet') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'delete':
if ($account->hasPermission('delete any aws cloud subnet')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('delete own aws cloud subnet') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* Not being used anymore.
*
* Access check is performed in
* \Drupal\cloud\Controller\CloudConfigController::access.
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add aws cloud subnet');
}
}
<?php
namespace Drupal\aws_cloud\Controller\Vpc;
use Drupal\cloud\Controller\CloudContentListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of Subnet.
*/
class SubnetListBuilder extends CloudContentListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = [];
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
return $row + parent::buildRow($entity);
}
}
<?php
namespace Drupal\aws_cloud\Entity\Vpc;
use Drupal\cloud\Entity\CloudContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Defines the Vpc entity.
*
* @ingroup aws_cloud
*
* @ContentEntityType(
* id = "aws_cloud_subnet",
* label = @Translation("AWS Cloud Subnet"),
* handlers = {
* "view_builder" = "Drupal\aws_cloud\Entity\Vpc\SubnetViewBuilder",
* "list_builder" = "Drupal\aws_cloud\Controller\Vpc\SubnetListBuilder",
* "views_data" = "Drupal\aws_cloud\Entity\Vpc\SubnetViewsData",
* "form" = {
* "default" = "Drupal\aws_cloud\Form\Vpc\SubnetEditForm",
* "add" = "Drupal\aws_cloud\Form\Vpc\SubnetCreateForm",
* "edit" = "Drupal\aws_cloud\Form\Vpc\SubnetEditForm" ,
* "delete" = "Drupal\aws_cloud\Form\Vpc\SubnetDeleteForm",
* "delete-multiple-confirm" = "Drupal\aws_cloud\Form\Vpc\SubnetDeleteMultipleForm",
* },
* "access" = "Drupal\aws_cloud\Controller\Vpc\SubnetAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "aws_cloud_subnet",
* admin_permission = "administer aws cloud subnet",
* fieldable = TRUE,
* entity_keys = {
* "id" = "id" ,
* "label" = "name",
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "/clouds/aws_cloud/{cloud_context}/subnet/{aws_cloud_subnet}",
* "edit-form" = "/clouds/aws_cloud/{cloud_context}/subnet/{aws_cloud_subnet}/edit",
* "delete-form" = "/clouds/aws_cloud/{cloud_context}/subnet/{aws_cloud_subnet}/delete",
* "collection" = "/clouds/aws_cloud/{cloud_context}/subnet",
* "delete-multiple-form" = "/clouds/aws_cloud/{cloud_context}/subnet/delete_multiple",
* },
* field_ui_base_route = "aws_cloud.vpc.settings"
* )
*/
class Subnet extends CloudContentEntityBase implements SubnetInterface {
/**
* {@inheritdoc}
*/
public function getSubnetId() {
return $this->get('subnet_id')->value;
}
/**
* {@inheritdoc}
*/
public function setSubnetId($subnet_id = '') {
return $this->set('subnet_id', $subnet_id);
}
/**
* {@inheritdoc}
*/
public function getVpcId() {
return $this->get('vpc_id')->value;
}
/**
* {@inheritdoc}
*/
public function setVpcId($vpc_id = '') {
return $this->set('vpc_id', $vpc_id);
}
/**
* {@inheritdoc}
*/
public function getCidrBlock() {
return $this->get('cidr_block')->value;
}
/**
* {@inheritdoc}
*/
public function setCidrBlock($cidr_block) {
return $this->set('cidr_block', $cidr_block);
}
/**
* {@inheritdoc}
*/
public function getAccountId() {
return $this->get('account_id')->value;
}
/**
* {@inheritdoc}
*/
public function setAccountId($account_id) {
return $this->set('account_id', $account_id);
}
/**
* {@inheritdoc}
*/
public function getState() {
return $this->get('state')->value;
}
/**
* {@inheritdoc}
*/
public function setState($state) {
return $this->set('state', $state);
}
/**
* {@inheritdoc}
*/
public function getTags() {
return $this->get('tags')->getValue();
}
/**
* {@inheritdoc}
*/
public function setTags(array $tags) {
return $this->set('tags', $tags);
}
/**
* {@inheritdoc}
*/
public function getRefreshed() {
return $this->get('refreshed')->value;
}
/**
* {@inheritdoc}
*/
public function setRefreshed($time) {
return $this->set('refreshed', $time);
}
/**
* {@inheritdoc}
*/
public function setCreated($created = 0) {
return $this->set('created', $created);
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the AwsCloudEc2Vpc entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the AwsCloudEc2Vpc entity.'))
->setReadOnly(TRUE);
$fields['cloud_context'] = BaseFieldDefinition::create('string')
->setRequired(TRUE)
->setLabel(t('Cloud ID'))
->setDescription(t('A unique machine name for the cloud provider.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the AwsCloudEc2Vpc entity.'))
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['subnet_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Subnet ID'))
->setDescription(t('The Subnet ID.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['vpc_id'] = BaseFieldDefinition::create('string')
->setLabel(t('VPC ID'))
->setDescription(t('The VPC ID.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['cidr_block'] = BaseFieldDefinition::create('string')
->setRequired(TRUE)
->setLabel(t('Cidr Block'))
->setDescription(t('Information about the IPv4 CIDR blocks associated with the VPC.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['state'] = BaseFieldDefinition::create('string')
->setLabel(t('State'))
->setDescription(t('The current state of the VPC.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['account_id'] = BaseFieldDefinition::create('string')
->setLabel(t('AWS Account ID'))
->setDescription(t('The AWS account ID of the vpc owner, without dashes.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
])
->setReadOnly(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('Date/time the Amazon vpc was created.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => -5,
'settings' => [
'date_format' => 'short',
],
]);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['refreshed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Refreshed'))
->setDescription(t('The time that the entity was last refreshed.'));
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of the AwsCloudEc2Subnet entity author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['tags'] = BaseFieldDefinition::create('tag')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDescription(t('Tags'))
->setDisplayOptions('view', [
'type' => 'tag_formatter',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'tag_item',
])
->addConstraint('tags_data');
return $fields;
}
}
<?php
namespace Drupal\aws_cloud\Entity\Vpc;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface defining an subnet entity.
*
* @ingroup aws_cloud
*/
interface SubnetInterface extends ContentEntityInterface, EntityOwnerInterface {
/**
* {@inheritdoc}
*/
public function getCloudContext();
/**
* Get the subnet ID.
*/
public function getSubnetId();
/**
* Set subnet ID.
*
* @param string $subnet_id
* The subnet ID.
*
* @return \Drupal\aws_cloud\Entity\Vpc\Subnet
* The Subnet Entity.
*/
public function setSubnetId($subnet_id = '');
/**
* Get the vpc ID.
*/
public function getVpcId();
/**
* Set vpc ID.
*
* @param string $vpc_id
* The vpc ID.
*
* @return \Drupal\aws_cloud\Entity\Vpc\Vpc
* The Vpc Entity.
*/
public function setVpcId($vpc_id = '');
/**
* Get the cidr block.
*/
public function getCidrBlock();
/**
* Set cidr block.
*
* @param string $cidr_block
* The cidr block.
*
* @return \Drupal\aws_cloud\Entity\Vpc\Vpc
* The Vpc Entity.
*/
public function setCidrBlock($cidr_block);
/**
* Get the tags.
*/
public function getTags();
/**
* Set the tags.
*
* @param array $tags
* The tags.
*
* @return \Drupal\aws_cloud\Entity\Vpc\Vpc
* The Vpc Entity.
*/
public function setTags(array $tags);
/**
* Get the account ID.
*/
public function getAccountId();
/**
* Set account ID.
*
* @param string $account_id
* The account ID.
*
* @return \Drupal\aws_cloud\Entity\Vpc\Vpc
* The Vpc Entity.
*/
public function setAccountId($account_id);
/**
* Get the state.
*/
public function getState();
/**
* Set state.
*
* @param string $state
* The state.
*
* @return \Drupal\aws_cloud\Entity\Vpc\Vpc
* The Vpc Entity.
*/
public function setState($state);
/**
* {@inheritdoc}
*/
public function created();
/**
* {@inheritdoc}
*/
public function setCreated($created = 0);
/**
* {@inheritdoc}
*/
public function changed();
/**
* {@inheritdoc}
*/
public function getRefreshed();
/**
* {@inheritdoc}
*/
public function setRefreshed($time);
}
<?php
namespace Drupal\aws_cloud\Entity\Vpc;
use Drupal\aws_cloud\Entity\Ec2\Ec2BaseViewBuilder;
/**
* Provides the subnet view builders.
*/
class SubnetViewBuilder extends Ec2BaseViewBuilder {
/**
* {@inheritdoc}
*/
protected function getFieldsetDefs() {
return [
[
'name' => 'subnet',
'title' => t('Subnet'),
'open' => TRUE,
'fields' => [
'subnet_id',
'vpc_id',
'state',
'account_id',
'created',
],
],
[
'name' => 'fieldset_tags',
'title' => t('Tags'),
'open' => TRUE,
'fields' => [
'tags',
],
],
[
'name' => 'others',
'title' => t('Others'),
'open' => FALSE,
'fields' => [
'cloud_context',
'uid',
],
],
];
}
}
<?php
namespace Drupal\aws_cloud\Entity\Vpc;
use Drupal\aws_cloud\Entity\Ec2\Ec2BaseViewsData;
/**
* Provides the views data for the Subnet entity type.
*/
class SubnetViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$data['aws_cloud_subnet']['table']['base'] = [
'field' => 'id',
'title' => t('AWS Cloud Subnet'),
'help' => t('The AWC Cloud Subnet entity ID.'),
];
$data['aws_cloud_subnet']['subnet_bulk_form'] = [
'title' => $this->t('Subnet operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple Subnets.'),
'field' => [
'id' => 'subnet_bulk_form',
],
];
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// The following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'subnet_id',
'vpc_id',
'state',
'account_id',
];
$data['aws_cloud_vpc']['table']['base']['access query tag'] = 'aws_cloud_subnet_views_access';
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
}
<?php
namespace Drupal\aws_cloud\Form\Vpc;
use Drupal\Core\Form\FormStateInterface;
use Drupal\aws_cloud\Form\Ec2\AwsCloudContentForm;
/**
* Form controller for the subnet entity create form.
*
* @ingroup aws_cloud
*/
class SubnetCreateForm extends AwsCloudContentForm {
/**
* Overrides Drupal\Core\Entity\EntityFormController::buildForm().
*/
public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
/* @var $entity \Drupal\aws_cloud\Entity\Ec2\Snapshot */
$form = parent::buildForm($form, $form_state);
$this->awsEc2Service->setCloudContext($cloud_context);
$entity = $this->entity;
$weight = -50;
$form['subnet'] = [
'#type' => 'details',
'#title' => $this->t('Subnet'),
'#open' => TRUE,
'#weight' => $weight++,
];
$form['subnet']['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#maxlength' => 255,
'#size' => 60,
'#required' => TRUE,
];
$vpcs = $this->awsEc2Service->getVpcs();
ksort($vpcs);
$form['subnet']['vpc_id'] = [
'#type' => 'select',
'#title' => $this->t('VPC CIDR (ID)'),
'#options' => $vpcs,
'#default_value' => $entity->getVpcId(),
'#weight' => -5,
'#required' => TRUE,
];
$form['subnet']['cidr_block'] = [
'#type' => 'textfield',
'#title' => $this->t('IPv4 CIDR block'),
'#description' => $this->t('The range of IPv4 addresses for your VPC in CIDR block format, for example, 10.0.0.0/24. Block sizes must be between a /16 netmask and /28 netmask.'),
'#maxlength' => 255,
'#size' => 60,
'#required' => TRUE,
];
$this->addOthersFieldset($form, $weight++, $cloud_context);
unset($form['tags']);
$form['actions'] = $this->actions($form, $form_state, $cloud_context);
return $form;
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
$cloud_context = $this->routeMatch->getParameter('cloud_context');
$entity = $this->entity;
$entity->setCloudContext($cloud_context);
$result = $this->awsEc2Service->createSubnet([
'VpcId' => $entity->getVpcId(),
'CidrBlock' => $entity->getCidrBlock(),
]);
if (isset($result['Subnet'])
&& ($entity->setSubnetId($result['Subnet']['SubnetId']))
&& ($entity->save())
) {
$this->setTagsInAws($entity->getSubnetId(), [
'subnet_created_by_uid' => $entity->getOwner()->id(),
'Name' => $entity->getName(),
]);
// Update the subnet.
$this->awsEc2Service->updateSubnets([
'SubnetId' => $entity->getSubnetId(),
]);
$message = $this->t('The @label "%label" has been created.', [
'@label' => $entity->getEntityType()->getLabel(),
'%label' => $entity->label(),
]);
$this->messenger->addMessage($message);
$form_state->setRedirect('view.aws_cloud_subnet.list', ['cloud_context' => $entity->getCloudContext()]);
}
else {
$message = $this->t('The @label "%label" couldn\'t create.', [
'@label' => $entity->getEntityType()->getLabel(),
'%label' => $entity->label(),
]);
$this->messenger->addError($message);
}
}
}
<?php
namespace Drupal\aws_cloud\Form\Vpc;
use Drupal\Core\Form\FormStateInterface;
use Drupal\aws_cloud\Form\Ec2\AwsDeleteForm;
/**
* Provides a form for deleting a Subnet entity.
*
* @ingroup aws_cloud
*/
class SubnetDeleteForm extends AwsDeleteForm {
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this->entity;
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
if ($this->awsEc2Service->deleteSubnet([
'SubnetId' => $entity->getSubnetId(),
]) != NULL) {
$message = $this->t('The @type "@label" has been deleted.', [
'@type' => $entity->getEntityType()->getLabel(),
'@label' => $entity->label(),
]);
$entity->delete();
$this->messenger->addMessage($message);
}
else {
$message = $this->t('The @type "@label" couldn\'t delete.', [
'@type' => $entity->getEntityType()->getLabel(),
'@label' => $entity->label(),
]);
$this->messenger->addError($message);
}
$form_state->setRedirect('view.aws_cloud_subnet.list', ['cloud_context' => $entity->getCloudContext()]);
}
}
<?php
namespace Drupal\aws_cloud\Form\Vpc;
use Drupal\cloud\Entity\CloudContentEntityBase;
use Drupal\aws_cloud\Form\Ec2\AwsCloudDeleteMultipleForm;
/**
* Provides an entities deletion confirmation form.
*/
class SubnetDeleteMultipleForm extends AwsCloudDeleteMultipleForm {
/**
* {@inheritdoc}
*/
protected function processAwsResource(CloudContentEntityBase $entity) {
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
return $this->awsEc2Service->deleteSubnet(
['SubnetId' => $entity->getSubnetId()]
) != NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment