Skip to content
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

Added API: Search Security Group corresponding to specific VPC. #1445

Merged
merged 4 commits into from
Feb 3, 2025
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
1 change: 1 addition & 0 deletions api-runtime/rest-runtime/CBSpiderRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func RunServer() {
{"POST", "/securitygroup", CreateSecurity},
{"GET", "/securitygroup", ListSecurity},
{"GET", "/securitygroup/:Name", GetSecurity},
{"GET", "/securitygroup/vpc/:VPCName", ListVpcSecurity},
{"DELETE", "/securitygroup/:Name", DeleteSecurity},
//-- for rule
{"POST", "/securitygroup/:SGName/rules", AddRules},
Expand Down
60 changes: 55 additions & 5 deletions api-runtime/rest-runtime/SecurityGroupRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package restruntime
import (
cmrt "github.com/cloud-barista/cb-spider/api-runtime/common-runtime"
cres "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
dri "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"

// REST API (echo)
"net/http"
Expand Down Expand Up @@ -56,7 +55,7 @@ func RegisterSecurity(c echo.Context) error {
}

// create UserIID
userIId := cres.IID{req.ReqInfo.Name, req.ReqInfo.CSPId}
userIId := cres.IID{NameId: req.ReqInfo.Name, SystemId: req.ReqInfo.CSPId}

// Call common-runtime API
result, err := cmrt.RegisterSecurity(req.ConnectionName, req.ReqInfo.VPCName, userIId)
Expand Down Expand Up @@ -111,7 +110,7 @@ type SecurityGroupCreateRequest struct {
Name string `json:"Name" validate:"required" example:"sg-01"`
VPCName string `json:"VPCName" validate:"required" example:"vpc-01"`
SecurityRules *[]cres.SecurityRuleInfo `json:"SecurityRules" validate:"required"`
TagList []dri.KeyValue `json:"TagList,omitempty" validate:"omitempty"`
TagList []cres.KeyValue `json:"TagList,omitempty" validate:"omitempty"`
} `json:"ReqInfo" validate:"required"`
}

Expand Down Expand Up @@ -139,8 +138,8 @@ func CreateSecurity(c echo.Context) error {

// Rest RegInfo => Driver ReqInfo
reqInfo := cres.SecurityReqInfo{
IId: cres.IID{req.ReqInfo.Name, req.ReqInfo.Name},
VpcIID: cres.IID{req.ReqInfo.VPCName, ""},
IId: cres.IID{NameId: req.ReqInfo.Name, SystemId: req.ReqInfo.Name},
VpcIID: cres.IID{NameId: req.ReqInfo.VPCName, SystemId: ""},
SecurityRules: req.ReqInfo.SecurityRules,
TagList: req.ReqInfo.TagList,
}
Expand Down Expand Up @@ -231,6 +230,57 @@ func ListAllSecurity(c echo.Context) error {
return c.JSON(http.StatusOK, &allResourceList)
}

// listVpcSecurity godoc
// @ID list-vpc-securitygroup
// @Summary List Security Groups in a Specific VPC
// @Description Retrieve a list of Security Groups associated with a specific VPC in a given cloud connection.
// @Tags [SecurityGroup Management]
// @Accept json
// @Produce json
// @Param VPCName path string true "The name of the VPC to list Security Groups for"
// @Param ConnectionName query string true "The name of the Connection to list Security Groups for"
// @Success 200 {object} restruntime.SecurityGroupListResponse "Response containing security groups under the key 'securitygroup'"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to missing parameters"
// @Failure 500 {object} SimpleMsg "Internal Server Error"
// @Router /vpcsecuritygroup/{VPCName} [get]
func ListVpcSecurity(c echo.Context) error {
cblog.Info("call ListVpcSecurity()")

req := ConnectionRequest{}

err := c.Bind(&req)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}

vpcName := c.Param("VpcName")
if vpcName == "" {
return echo.NewHTTPError(http.StatusBadRequest, "VPCName is required")
}

allSecurityGroups, err := cmrt.ListSecurity(req.ConnectionName, SG)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

filteredSecurityGroups := make([]*cres.SecurityInfo, 0)
for _, sg := range allSecurityGroups {
if sg.IId.NameId != "" && sg.VpcIID.NameId != "" && sg.VpcIID.NameId == vpcName {
filteredSecurityGroups = append(filteredSecurityGroups, sg)
}
}

jsonResult := SecurityGroupListResponse{
Result: filteredSecurityGroups,
}

return c.JSON(http.StatusOK, &jsonResult)
}

// listAllSecurityGroupInfo godoc
// @ID list-all-securitygroup-info
// @Summary List All SecurityGroup Info
Expand Down
24 changes: 9 additions & 15 deletions api-runtime/rest-runtime/admin-web/html/vm.html
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ <h2>Add New VM</h2>

<div class="form-group">
<label for="vpcSelect">VPC:</label>
<select id="vpcSelect" name="vpcSelect" required onchange="updateSubnets()" style="background-color: #f3f3fd;">
<select id="vpcSelect" name="vpcSelect" required onchange="updateSubnets(); loadSecurityGroups();" style="background-color: #f3f3fd;">
</select>
</div>

Expand Down Expand Up @@ -1352,15 +1352,7 @@ <h2>System ID (Managed by CSP)</h2>
}

function showOverlay() {
const region = '{{.RegionName}}';
const vmNameInput = document.getElementById('vmName');

if (vmNameInput) {
vmNameInput.value = `${region}-vm-${Math.random().toString(36).substring(2, 8)}`;
} else {
console.error("Element with ID 'vmName' not found.");
}

console.log("showOverlay()");
document.getElementById('overlay').style.display = 'flex';
document.addEventListener('keydown', handleEsc);
clearFormFields();
Expand All @@ -1370,11 +1362,12 @@ <h2>System ID (Managed by CSP)</h2>
const region = '{{.RegionName}}';
const vmNameInput = document.getElementById('vmName');
vmNameInput.value = `${region}-vm-${Math.random().toString(36).substring(2, 5)}`;

loadSecurityGroups();
const tagContainer = document.getElementById('vm-tag-container');
while (tagContainer.firstChild) {
tagContainer.removeChild(tagContainer.firstChild);
}

}

function hideOverlay() {
Expand Down Expand Up @@ -1666,7 +1659,7 @@ <h2>System ID (Managed by CSP)</h2>
if (connConfigElement) {
loadVPCs();
loadKeyPairs();
loadSecurityGroups();
//loadSecurityGroups();
} else {
console.error("Error: 'connConfig' element not found.");
}
Expand Down Expand Up @@ -1719,7 +1712,7 @@ <h2>System ID (Managed by CSP)</h2>
const vpcSelect = document.getElementById('vpcSelect');
const selectedSubnets = JSON.parse(vpcSelect.value);
const subnetSelect = document.getElementById('subnetSelect');
const dataDiskSelect = document.getElementById('dataDiskSelect'); // here
const dataDiskSelect = document.getElementById('dataDiskSelect');

subnetSelect.innerHTML = '';

Expand Down Expand Up @@ -1777,13 +1770,15 @@ <h2>System ID (Managed by CSP)</h2>
function loadSecurityGroups() {
const connConfig = document.getElementById('connConfig').value;
const securityGroupSelect = document.getElementById('securityGroupSelect');
const vpcName = document.getElementById('vpcSelect').options[document.getElementById('vpcSelect').selectedIndex].text.split(' ')[0];
const url = `/spider/securitygroup/vpc/${vpcName}?ConnectionName=${connConfig}`

if (!securityGroupSelect) {
console.error('Security Group select element not found');
return;
}

fetch(`/spider/securitygroup?ConnectionName=${connConfig}`, {
fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
Expand Down Expand Up @@ -2085,7 +2080,6 @@ <h2>SSH Access</h2>
.then(data => {
const diskSelect = document.getElementById('dataDiskSelect');
diskSelect.innerHTML = '';
console.log(data);

const filteredDisks = data.disk.filter(disk => disk.Zone === zone && disk.Status === "Available");

Expand Down
52 changes: 52 additions & 0 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8359,6 +8359,58 @@ const docTemplate = `{
}
}
}
},
"/vpcsecuritygroup/{VPCName}": {
"get": {
"description": "Retrieve a list of Security Groups associated with a specific VPC in a given cloud connection.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[SecurityGroup Management]"
],
"summary": "List Security Groups in a Specific VPC",
"operationId": "list-vpc-securitygroup",
"parameters": [
{
"type": "string",
"description": "The name of the VPC to list Security Groups for",
"name": "VPCName",
"in": "path",
"required": true
},
{
"type": "string",
"description": "The name of the Connection to list Security Groups for",
"name": "ConnectionName",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "Response containing security groups under the key 'securitygroup'",
"schema": {
"$ref": "#/definitions/spider.SecurityGroupListResponse"
}
},
"400": {
"description": "Bad Request, possibly due to missing parameters",
"schema": {
"$ref": "#/definitions/spider.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/spider.SimpleMsg"
}
}
}
}
}
},
"definitions": {
Expand Down
52 changes: 52 additions & 0 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -8356,6 +8356,58 @@
}
}
}
},
"/vpcsecuritygroup/{VPCName}": {
"get": {
"description": "Retrieve a list of Security Groups associated with a specific VPC in a given cloud connection.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[SecurityGroup Management]"
],
"summary": "List Security Groups in a Specific VPC",
"operationId": "list-vpc-securitygroup",
"parameters": [
{
"type": "string",
"description": "The name of the VPC to list Security Groups for",
"name": "VPCName",
"in": "path",
"required": true
},
{
"type": "string",
"description": "The name of the Connection to list Security Groups for",
"name": "ConnectionName",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "Response containing security groups under the key 'securitygroup'",
"schema": {
"$ref": "#/definitions/spider.SecurityGroupListResponse"
}
},
"400": {
"description": "Bad Request, possibly due to missing parameters",
"schema": {
"$ref": "#/definitions/spider.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/spider.SimpleMsg"
}
}
}
}
}
},
"definitions": {
Expand Down
36 changes: 36 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8675,6 +8675,42 @@ paths:
summary: Get Subnet
tags:
- '[VPC Management]'
/vpcsecuritygroup/{VPCName}:
get:
consumes:
- application/json
description: Retrieve a list of Security Groups associated with a specific VPC
in a given cloud connection.
operationId: list-vpc-securitygroup
parameters:
- description: The name of the VPC to list Security Groups for
in: path
name: VPCName
required: true
type: string
- description: The name of the Connection to list Security Groups for
in: query
name: ConnectionName
required: true
type: string
produces:
- application/json
responses:
"200":
description: Response containing security groups under the key 'securitygroup'
schema:
$ref: '#/definitions/spider.SecurityGroupListResponse'
"400":
description: Bad Request, possibly due to missing parameters
schema:
$ref: '#/definitions/spider.SimpleMsg'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/spider.SimpleMsg'
summary: List Security Groups in a Specific VPC
tags:
- '[SecurityGroup Management]'
schemes:
- http
securityDefinitions:
Expand Down
Loading