Skip to content

Commit

Permalink
[Server] add domain additional resource list
Browse files Browse the repository at this point in the history
  • Loading branch information
roryye authored and SongZhen0704 committed Dec 5, 2023
1 parent 1e3b492 commit 419d254
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 3 deletions.
143 changes: 143 additions & 0 deletions cli/ctl/domain_additional_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ctl

import (
"fmt"
"html/template"
"io/ioutil"
"os"

Expand Down Expand Up @@ -57,7 +58,24 @@ func RegisterDomainAdditionalResourceCommand() *cobra.Command {
},
}

var resourceType, resourceName string
list := &cobra.Command{
Use: "list",
Short: "list domain additional resource",
Example: "deepflow-ctl domain additional-resource list",
Run: func(cmd *cobra.Command, args []string) {
if resourceName != "" && resourceType == "" {
fmt.Printf("please enter resource type, resource name(%v)\n", resourceName)
return
}
listDomainAdditionalResource(cmd, resourceType, resourceName)
},
}
list.Flags().StringVarP(&resourceType, "type", "", "", "resource type, support: az, vpc, subnet, host, chonst, lb")
list.Flags().StringVarP(&resourceName, "name", "", "", "resource name")

DomainAdditionalResource.AddCommand(apply)
DomainAdditionalResource.AddCommand(list)
DomainAdditionalResource.AddCommand(exampleCmd)
return DomainAdditionalResource
}
Expand All @@ -77,6 +95,131 @@ func applyDomainAdditionalResource(cmd *cobra.Command, args []string, filename s
}
}

var additionalListTemplate = `{{- if .AZS }}
azs:{{ range .AZS }}
- name: {{ .NAME }}
uuid: {{ .UUID }}
domain_uuid: {{ .DOMAIN_UUID }}
{{- end }}{{ end }}
{{- if .VPCS }}
vpcs:{{ range .VPCS }}
- name: {{ .NAME }}
uuid: {{ .UUID }}
domain_uuid: {{ .DOMAIN_UUID }}
{{- end }}{{ end }}
{{- if .SUBNETS }}
subnets:{{ range .SUBNETS }}
- name: {{ .NAME }}
uuid: {{ .UUID }}
type: {{ .TYPE }}
is_vip: {{ .IS_VIP }}
vpc_uuid: {{ .VPC_UUID }}
{{- if .AZ_UUID }}az_uuid: {{ .AZ_UUID }}{{ end }}
domain_uuid: {{ .DOMAIN_UUID }}
cidrs:{{ range .CIDRS }}
- {{ . }}
{{- end }}
{{- end }}{{ end }}
{{- if .HOSTS }}
hosts:{{ range .HOSTS }}
- name: {{ .NAME }}
uuid: {{ .UUID }}
ip: {{ .IP }}
type: {{ .TYPE }}
az_uuid: {{ .AZ_UUID }}
domain_uuid: {{ .DOMAIN_UUID }}
{{- if .VINTERFACES }}
vinterfaces: {{ range .VINTERFACES }}
- mac: {{ .MAC }}
name: {{ .NAME }}
subnet_uuid: {{ .SUBNET_UUID }}
{{- if .IPS }}
ips:{{ range .IPS }}
- {{ . }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}{{- end }}
{{- if .CHOSTS }}
chosts:{{ range .CHOSTS }}
- name: {{ .NAME }}
uuid: {{ .UUID }}
host_ip: {{ .HOST_IP }}
type: {{ .TYPE }}
vpc_uuid: {{ .VPC_UUID }}
az_uuid: {{ .AZ_UUID }}
domain_uuid: {{ .DOMAIN_UUID }}
{{- if .VINTERFACES }}
vinterfaces: {{ range .VINTERFACES }}
- mac: {{ .MAC }}
subnet_uuid: {{ .SUBNET_UUID }}
{{- if .IPS }}
ips:{{ range .IPS }}
- {{ . }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}{{- end }}
{{- if .LBS }}
lbs:{{ range .LBS }}
- name: {{ .NAME }}
model: {{ .MODEL }}
vpc_uuid: {{ .VPC_UUID }}
domain_uuid: {{ .DOMAIN_UUID }}
region_uuid: {{ .REGION_UUID }}
{{- if .VINTERFACES }}
vinterfaces: {{ range .VINTERFACES }}
- mac: {{ .MAC }}
subnet_uuid: {{ .SUBNET_UUID }}
{{- if .IPS }}
ips:{{ range .IPS}}
- {{ . }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- if .LB_LISTENERS }}
lb_listeners:{{ range .LB_LISTENERS }}
- {{ if .NAME }}name: {{ .NAME }}{{ end }}
protocol: {{ .PROTOCOL }}
ip: {{ .IP }}
port: {{ .PORT }}
{{ if .LB_TARGET_SERVERS }}lb_target_servers:{{range .LB_TARGET_SERVERS }}
- ip: {{ .IP }}
port: {{ .PORT }}
{{ end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}{{ end }}
`

func listDomainAdditionalResource(cmd *cobra.Command, resourceType, resourceName string) {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/domain-additional-resources/?type=%s&name=%s", server.IP, server.Port, resourceType, resourceName)
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}

data, err := response.Get("DATA").Map()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}

t := template.Must(template.New("domain_additional_list").Parse(additionalListTemplate))
t.Execute(os.Stdout, data)
}

func exampleDomainAdditionalResourceConfig(cmd *cobra.Command) {
fmt.Printf(string(example.YamlDomainAdditionalResourceReader))
}
Expand Down
20 changes: 20 additions & 0 deletions server/controller/http/router/resource/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (d *Domain) RegisterTo(e *gin.Engine) {
e.DELETE("/v2/sub-domains/:lcuuid/", deleteSubDomain)

e.PUT("/v1/domain-additional-resources/", applyDomainAddtionalResource)
e.GET("/v1/domain-additional-resources/", listDomainAddtionalResource)
}

func getDomain(c *gin.Context) {
Expand Down Expand Up @@ -242,3 +243,22 @@ func applyDomainAddtionalResource(c *gin.Context) {
err = resource.ApplyDomainAddtionalResource(data)
common.JsonResponse(c, map[string]interface{}{}, err)
}

func listDomainAddtionalResource(c *gin.Context) {
var resourceType, resourceName string
t, ok := c.GetQuery("type")
if ok {
resourceType = t
}
name, ok := c.GetQuery("name")
if ok {
resourceName = name
}
if resourceName != "" && resourceType == "" {
common.JsonResponse(c, httpcommon.PARAMETER_ILLEGAL, fmt.Errorf("please enter resource type, resource name(%v)", resourceName))
return
}

data, err := resource.ListDomainAdditionalResource(resourceType, resourceName)
common.JsonResponse(c, data, err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ func generateCloudModelData(domainUUIDToToolDataSet map[string]*addtionalResourc
VPCLcuuid: subnet.VPCUUID,
AZLcuuid: subnet.AZUUID,
RegionLcuuid: toolDS.regionUUID,
IsVIP: subnet.IsVIP,
},
)
for _, cidr := range subnet.CIDRs {
Expand Down
Loading

0 comments on commit 419d254

Please sign in to comment.