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

PET-211: add name field for floatingip #370

Merged
merged 1 commit into from
Dec 24, 2024
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
9 changes: 7 additions & 2 deletions web/src/apis/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type FloatingIpAPI struct{}
type FloatingIpInfo struct {
*BaseReference
IpAddress string `json:"ip_address"`
Name string `json:"name"`
}

type TargetInterface struct {
Expand All @@ -44,6 +45,7 @@ type InstanceInfo struct {
type FloatingIpResponse struct {
*BaseID
PublicIp string `json:"public_ip"`
Name string `json:"name"`
TargetInterface *TargetInterface `json:"target_interface,omitempty"`
VPC *BaseReference `json:"vpc,omitempty"`
}
Expand All @@ -58,6 +60,7 @@ type FloatingIpListResponse struct {
type FloatingIpPayload struct {
PublicSubnet *BaseReference `json:"public_subnet" binding:"omitempty"`
PublicIp string `json:"public_ip" binding:"omitempty,ipv4"`
Name string `json:"name" binding:"omitempty"`
Instance *BaseID `json:"instance" binding:"omitempty"`
Bandwidth int64 `json:"bandwidth" binding:"omitempty"`
}
Expand Down Expand Up @@ -201,7 +204,7 @@ func (v *FloatingIpAPI) Create(c *gin.Context) {
return
}
}
floatingIp, err := floatingIpAdmin.Create(ctx, instance, publicSubnet, payload.PublicIp)
floatingIp, err := floatingIpAdmin.Create(ctx, instance, publicSubnet, payload.PublicIp, payload.Name)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, "Failed to create floating ip", err)
return
Expand All @@ -220,6 +223,7 @@ func (v *FloatingIpAPI) getFloatingIpResponse(ctx context.Context, floatingIp *m
ID: floatingIp.UUID,
},
PublicIp: floatingIp.FipAddress,
Name: floatingIp.Name,
}
if floatingIp.Router != nil {
floatingIpResp.VPC = &BaseReference{
Expand Down Expand Up @@ -258,6 +262,7 @@ func (v *FloatingIpAPI) List(c *gin.Context) {
ctx := c.Request.Context()
offsetStr := c.DefaultQuery("offset", "0")
limitStr := c.DefaultQuery("limit", "50")
queryStr := c.DefaultQuery("query", "")
offset, err := strconv.Atoi(offsetStr)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, "Invalid query offset: "+offsetStr, err)
Expand All @@ -272,7 +277,7 @@ func (v *FloatingIpAPI) List(c *gin.Context) {
ErrorResponse(c, http.StatusBadRequest, "Invalid query offset or limit", err)
return
}
total, floatingIps, err := floatingIpAdmin.List(ctx, int64(offset), int64(limit), "-created_at", "")
total, floatingIps, err := floatingIpAdmin.List(ctx, int64(offset), int64(limit), "-created_at", queryStr)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, "Failed to list floatingIps", err)
return
Expand Down
3 changes: 2 additions & 1 deletion web/src/model/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

type FloatingIp struct {
Model
Owner int64 `gorm:"default:1"` /* The organization ID of the resource */
Owner int64 `gorm:"default:1"` /* The organization ID of the resource */
Name string `gorm:"type:varchar(64)",gorm:"index"` /* The name of the resource */
FipAddress string `gorm:"type:varchar(64)"`
IntAddress string `gorm:"type:varchar(64)"`
InstanceID int64
Expand Down
9 changes: 5 additions & 4 deletions web/src/routes/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type FloatingIps struct {
type FloatingIpAdmin struct{}
type FloatingIpView struct{}

func (a *FloatingIpAdmin) Create(ctx context.Context, instance *model.Instance, pubSubnet *model.Subnet, publicIp string) (floatingIp *model.FloatingIp, err error) {
func (a *FloatingIpAdmin) Create(ctx context.Context, instance *model.Instance, pubSubnet *model.Subnet, publicIp string, name string) (floatingIp *model.FloatingIp, err error) {
memberShip := GetMemberShip(ctx)
permit := memberShip.CheckPermission(model.Writer)
if !permit {
Expand All @@ -59,7 +59,7 @@ func (a *FloatingIpAdmin) Create(ctx context.Context, instance *model.Instance,
db.Rollback()
}
}()
floatingIp = &model.FloatingIp{Model: model.Model{Creater: memberShip.UserID}, Owner: memberShip.OrgID}
floatingIp = &model.FloatingIp{Model: model.Model{Creater: memberShip.UserID}, Owner: memberShip.OrgID, Name: name}
err = db.Create(floatingIp).Error
if err != nil {
log.Println("DB failed to create floating ip", err)
Expand Down Expand Up @@ -285,7 +285,7 @@ func (a *FloatingIpAdmin) List(ctx context.Context, offset, limit int64, order,
order = "created_at"
}
if query != "" {
query = fmt.Sprintf("fip_address like '%%%s%%' or int_address like '%%%s%%'", query, query)
query = fmt.Sprintf("fip_address like '%%%s%%' or int_address like '%%%s%%' or name like '%%%s%%'", query, query)
}

db := DB()
Expand Down Expand Up @@ -455,14 +455,15 @@ func (v *FloatingIpView) Create(c *macaron.Context, store session.Store) {
redirectTo := "../floatingips"
instID := c.QueryInt64("instance")
publicIp := c.QueryTrim("publicip")
name := c.QueryTrim("name")
instance, err := instanceAdmin.Get(ctx, int64(instID))
if err != nil {
log.Println("Failed to get instance ", err)
c.Data["ErrorMsg"] = err.Error()
c.HTML(500, "500")
return
}
_, err = floatingIpAdmin.Create(c.Req.Context(), instance, nil, publicIp)
_, err = floatingIpAdmin.Create(c.Req.Context(), instance, nil, publicIp, name)
if err != nil {
log.Println("Failed to create floating ip", err)
c.Data["ErrorMsg"] = err.Error()
Expand Down
2 changes: 2 additions & 0 deletions web/templates/floatingips.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{{ if $.IsAdmin }}
<th>{{.i18n.Tr "ID"}}</th>
{{ end }}
<th>{{.i18n.Tr "Name"}}</th>
<th>{{.i18n.Tr "FloatingIP"}}</th>
<th>{{.i18n.Tr "InternalIP"}}</th>
<th>{{.i18n.Tr "Instance"}}</th>
Expand All @@ -39,6 +40,7 @@
{{ if $.IsAdmin }}
<td>{{.ID}}</td>
{{ end }}
<td>{{.Name}}</td>
<td>{{.FipAddress}}</td>
<td>{{.IntAddress}}</td>
<td>
Expand Down
4 changes: 4 additions & 0 deletions web/templates/floatingips_new.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
{{.i18n.Tr "Create New Floating Ip"}}
</h3>
<div class="ui attached segment">
<div class="required inline field">
<label for="name">{{.i18n.Tr "Name"}}</label>
<input id="name" name="name" placeholder="{{.i18n.Tr "Name"}}">
</div>
<div class="required inline field">
<label for="instance">{{.i18n.Tr "Instance Address"}}</label>
<div class="ui selection dropdown">
Expand Down