Compare commits

..

No commits in common. "c08de789840f95065f407dc0c75e42a32cdc2b4a" and "e0190f4bb5a23db01a0219bbae5004436ac35e54" have entirely different histories.

10 changed files with 123 additions and 279 deletions

View File

@ -6,7 +6,6 @@ import (
"strings" "strings"
"git.beisel.it/florian/hostname-service/db" "git.beisel.it/florian/hostname-service/db"
"git.beisel.it/florian/hostname-service/models"
"git.beisel.it/florian/hostname-service/rules" "git.beisel.it/florian/hostname-service/rules"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -66,8 +65,7 @@ func CreateOrUpdateHostname(c *gin.Context, isUpdate bool) {
return return
} }
response := models.SimpleHostnameResponse{Hostname: hostname} c.JSON(http.StatusOK, gin.H{"hostname": hostname})
c.JSON(http.StatusOK, response)
} }
// @Summary Delete a hostname from the database // @Summary Delete a hostname from the database
@ -76,7 +74,7 @@ func CreateOrUpdateHostname(c *gin.Context, isUpdate bool) {
// @Produce json // @Produce json
// @Param category path string true "Category of the hostname" // @Param category path string true "Category of the hostname"
// @Param hostname path string true "Hostname to delete" // @Param hostname path string true "Hostname to delete"
// @Success 200 {object} models.SimpleHostnameResponse "Hostname" // @Success 200 {json} json "Hostname"
// @Security Bearer // @Security Bearer
// @Tags Manipulate existing Hostnames // @Tags Manipulate existing Hostnames
// @Router /{category}/{hostname} [delete] // @Router /{category}/{hostname} [delete]
@ -89,8 +87,7 @@ func DeleteHostname(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
} }
response := models.SimpleHostnameResponse{Hostname: hostname} c.JSON(http.StatusOK, gin.H{"hostname": hostname})
c.JSON(http.StatusOK, response)
} }
// @Summary Return a list of hosts and their details filtered by category // @Summary Return a list of hosts and their details filtered by category
@ -98,7 +95,7 @@ func DeleteHostname(c *gin.Context) {
// @ID list-hostnames-by-category // @ID list-hostnames-by-category
// @Produce json // @Produce json
// @Param category path string true "Category of the hostname" // @Param category path string true "Category of the hostname"
// @Success 200 {array} models.Hostname "An array of responses" // @Success 200 {json} json "Hostname"
// @Security Bearer // @Security Bearer
// @Tags Querying Hostnames // @Tags Querying Hostnames
// @Router /{category} [get] // @Router /{category} [get]
@ -123,8 +120,8 @@ func ListHostnamesByCategory(c *gin.Context) {
// @Produce json // @Produce json
// @Param category path string true "Category of the hostname" // @Param category path string true "Category of the hostname"
// @Param hostname path string true "Category of the hostname" // @Param hostname path string true "Category of the hostname"
// @Success 200 {object} models.Hostname "A single response object"
// @Security Bearer // @Security Bearer
// @Success 200 {json} json "Hostname"
// @Tags Querying Hostnames // @Tags Querying Hostnames
// @Router /{category}/{hostname} [get] // @Router /{category}/{hostname} [get]
func GetHostnameByCategoryAndName(c *gin.Context) { func GetHostnameByCategoryAndName(c *gin.Context) {

View File

@ -12,17 +12,16 @@ import (
) )
// LoginHandler godoc // LoginHandler godoc
//
// @Summary User login // @Summary User login
// @Description Authenticate user and return JWT token // @Description Authenticate user and return JWT token
// @Tags Authentication // @Tags Authentication
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param loginCredentials body models.LoginCredentials true "Login Credentials" // @Param loginCredentials body models.LoginCredentials true "Login Credentials"
// @Success 200 {object} models.TokenResponse "Successfully authenticated, JWT token returned" // @Success 200 {object} map[string]string "Successfully authenticated, JWT token returned"
// @Failure 400 {object} models.ErrorResponse "Invalid request body" // @Failure 400 {object} map[string]string "Invalid request body"
// @Failure 401 {object} models.ErrorResponse "Invalid login credentials" // @Failure 401 {object} map[string]string "Invalid login credentials"
// @Failure 500 {object} models.ErrorResponse "Internal server error" // @Failure 500 {object} map[string]string "Internal server error"
// @Router /login [post] // @Router /login [post]
func LoginHandler(c *gin.Context) { func LoginHandler(c *gin.Context) {
var creds models.LoginCredentials var creds models.LoginCredentials

View File

@ -52,7 +52,7 @@ const docTemplate = `{
"200": { "200": {
"description": "Hostname", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse" "type": "string"
} }
} }
} }
@ -85,7 +85,7 @@ const docTemplate = `{
"200": { "200": {
"description": "Hostname", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse" "type": "string"
} }
} }
} }
@ -148,25 +148,37 @@ const docTemplate = `{
"200": { "200": {
"description": "Successfully authenticated, JWT token returned", "description": "Successfully authenticated, JWT token returned",
"schema": { "schema": {
"$ref": "#/definitions/models.TokenResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
}, },
"400": { "400": {
"description": "Invalid request body", "description": "Invalid request body",
"schema": { "schema": {
"$ref": "#/definitions/models.ErrorResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
}, },
"401": { "401": {
"description": "Invalid login credentials", "description": "Invalid login credentials",
"schema": { "schema": {
"$ref": "#/definitions/models.ErrorResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
}, },
"500": { "500": {
"description": "Internal server error", "description": "Internal server error",
"schema": { "schema": {
"$ref": "#/definitions/models.ErrorResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
} }
} }
@ -199,12 +211,9 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "An array of responses", "description": "Hostname",
"schema": { "schema": {
"type": "array", "type": "json"
"items": {
"$ref": "#/definitions/models.Hostname"
}
} }
} }
} }
@ -244,9 +253,9 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "A single response object", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.Hostname" "type": "json"
} }
} }
} }
@ -286,7 +295,7 @@ const docTemplate = `{
"200": { "200": {
"description": "Hostname", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse" "type": "json"
} }
} }
} }
@ -294,72 +303,14 @@ const docTemplate = `{
} }
}, },
"definitions": { "definitions": {
"models.ErrorResponse": {
"type": "object",
"properties": {
"error": {
"type": "string"
}
}
},
"models.Hostname": {
"description": "Model of the Hostname as it is represented in the database",
"type": "object",
"properties": {
"Category": {
"description": "Category / Rule that was used when generating the hostname",
"type": "string",
"example": "notebook"
},
"Created_at": {
"description": "Creation Time of the entry",
"type": "string",
"example": "2024-01-16T12:53:59Z"
},
"Hostname": {
"description": "Generated hostname",
"type": "string",
"example": "ISEHENNB0009"
},
"Id": {
"description": "Internal ID of the Hostname within the database",
"type": "integer",
"example": 25
},
"Parameters": {
"description": "Parameter object of rule specific attributes, see rule.* models",
"type": "object",
"additionalProperties": true
}
}
},
"models.LoginCredentials": { "models.LoginCredentials": {
"description": "User account information used in the login process with Username and password", "description": "User account information used in the login process with Username and password",
"type": "object", "type": "object",
"properties": { "properties": {
"Password": { "password": {
"type": "string" "type": "string"
}, },
"Username": { "username": {
"type": "string"
}
}
},
"models.SimpleHostnameResponse": {
"description": "Model of the Hostname as returned by POST endpoint",
"type": "object",
"properties": {
"Hostname": {
"description": "Name of the newly generated host",
"type": "string"
}
}
},
"models.TokenResponse": {
"description": "Model returned after successful login",
"type": "object",
"properties": {
"token": {
"type": "string" "type": "string"
} }
} }

View File

@ -46,7 +46,7 @@
"200": { "200": {
"description": "Hostname", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse" "type": "string"
} }
} }
} }
@ -79,7 +79,7 @@
"200": { "200": {
"description": "Hostname", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse" "type": "string"
} }
} }
} }
@ -142,25 +142,37 @@
"200": { "200": {
"description": "Successfully authenticated, JWT token returned", "description": "Successfully authenticated, JWT token returned",
"schema": { "schema": {
"$ref": "#/definitions/models.TokenResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
}, },
"400": { "400": {
"description": "Invalid request body", "description": "Invalid request body",
"schema": { "schema": {
"$ref": "#/definitions/models.ErrorResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
}, },
"401": { "401": {
"description": "Invalid login credentials", "description": "Invalid login credentials",
"schema": { "schema": {
"$ref": "#/definitions/models.ErrorResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
}, },
"500": { "500": {
"description": "Internal server error", "description": "Internal server error",
"schema": { "schema": {
"$ref": "#/definitions/models.ErrorResponse" "type": "object",
"additionalProperties": {
"type": "string"
}
} }
} }
} }
@ -193,12 +205,9 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "An array of responses", "description": "Hostname",
"schema": { "schema": {
"type": "array", "type": "json"
"items": {
"$ref": "#/definitions/models.Hostname"
}
} }
} }
} }
@ -238,9 +247,9 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "A single response object", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.Hostname" "type": "json"
} }
} }
} }
@ -280,7 +289,7 @@
"200": { "200": {
"description": "Hostname", "description": "Hostname",
"schema": { "schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse" "type": "json"
} }
} }
} }
@ -288,72 +297,14 @@
} }
}, },
"definitions": { "definitions": {
"models.ErrorResponse": {
"type": "object",
"properties": {
"error": {
"type": "string"
}
}
},
"models.Hostname": {
"description": "Model of the Hostname as it is represented in the database",
"type": "object",
"properties": {
"Category": {
"description": "Category / Rule that was used when generating the hostname",
"type": "string",
"example": "notebook"
},
"Created_at": {
"description": "Creation Time of the entry",
"type": "string",
"example": "2024-01-16T12:53:59Z"
},
"Hostname": {
"description": "Generated hostname",
"type": "string",
"example": "ISEHENNB0009"
},
"Id": {
"description": "Internal ID of the Hostname within the database",
"type": "integer",
"example": 25
},
"Parameters": {
"description": "Parameter object of rule specific attributes, see rule.* models",
"type": "object",
"additionalProperties": true
}
}
},
"models.LoginCredentials": { "models.LoginCredentials": {
"description": "User account information used in the login process with Username and password", "description": "User account information used in the login process with Username and password",
"type": "object", "type": "object",
"properties": { "properties": {
"Password": { "password": {
"type": "string" "type": "string"
}, },
"Username": { "username": {
"type": "string"
}
}
},
"models.SimpleHostnameResponse": {
"description": "Model of the Hostname as returned by POST endpoint",
"type": "object",
"properties": {
"Hostname": {
"description": "Name of the newly generated host",
"type": "string"
}
}
},
"models.TokenResponse": {
"description": "Model returned after successful login",
"type": "object",
"properties": {
"token": {
"type": "string" "type": "string"
} }
} }

View File

@ -1,54 +1,12 @@
basePath: /api/v1 basePath: /api/v1
definitions: definitions:
models.ErrorResponse:
properties:
error:
type: string
type: object
models.Hostname:
description: Model of the Hostname as it is represented in the database
properties:
Category:
description: Category / Rule that was used when generating the hostname
example: notebook
type: string
Created_at:
description: Creation Time of the entry
example: "2024-01-16T12:53:59Z"
type: string
Hostname:
description: Generated hostname
example: ISEHENNB0009
type: string
Id:
description: Internal ID of the Hostname within the database
example: 25
type: integer
Parameters:
additionalProperties: true
description: Parameter object of rule specific attributes, see rule.* models
type: object
type: object
models.LoginCredentials: models.LoginCredentials:
description: User account information used in the login process with Username description: User account information used in the login process with Username
and password and password
properties: properties:
Password: password:
type: string type: string
Username: username:
type: string
type: object
models.SimpleHostnameResponse:
description: Model of the Hostname as returned by POST endpoint
properties:
Hostname:
description: Name of the newly generated host
type: string
type: object
models.TokenResponse:
description: Model returned after successful login
properties:
token:
type: string type: string
type: object type: object
rules.NotebookRuleInput: rules.NotebookRuleInput:
@ -85,11 +43,9 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: An array of responses description: Hostname
schema: schema:
items: type: json
$ref: '#/definitions/models.Hostname'
type: array
security: security:
- Bearer: [] - Bearer: []
summary: Return a list of hosts and their details filtered by category summary: Return a list of hosts and their details filtered by category
@ -116,7 +72,7 @@ paths:
"200": "200":
description: Hostname description: Hostname
schema: schema:
$ref: '#/definitions/models.SimpleHostnameResponse' type: json
security: security:
- Bearer: [] - Bearer: []
summary: Delete a hostname from the database summary: Delete a hostname from the database
@ -140,9 +96,9 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: A single response object description: Hostname
schema: schema:
$ref: '#/definitions/models.Hostname' type: json
security: security:
- Bearer: [] - Bearer: []
summary: Return a single hostname by Category and Name summary: Return a single hostname by Category and Name
@ -167,7 +123,7 @@ paths:
"200": "200":
description: Hostname description: Hostname
schema: schema:
$ref: '#/definitions/models.SimpleHostnameResponse' type: string
summary: Generate hostname for category "notebook" summary: Generate hostname for category "notebook"
tags: tags:
- Generating Hostnames - Generating Hostnames
@ -189,7 +145,7 @@ paths:
"200": "200":
description: Hostname description: Hostname
schema: schema:
$ref: '#/definitions/models.SimpleHostnameResponse' type: string
summary: Update hostname for category "notebook" summary: Update hostname for category "notebook"
tags: tags:
- Generating Hostnames - Generating Hostnames
@ -229,19 +185,27 @@ paths:
"200": "200":
description: Successfully authenticated, JWT token returned description: Successfully authenticated, JWT token returned
schema: schema:
$ref: '#/definitions/models.TokenResponse' additionalProperties:
type: string
type: object
"400": "400":
description: Invalid request body description: Invalid request body
schema: schema:
$ref: '#/definitions/models.ErrorResponse' additionalProperties:
type: string
type: object
"401": "401":
description: Invalid login credentials description: Invalid login credentials
schema: schema:
$ref: '#/definitions/models.ErrorResponse' additionalProperties:
type: string
type: object
"500": "500":
description: Internal server error description: Internal server error
schema: schema:
$ref: '#/definitions/models.ErrorResponse' additionalProperties:
type: string
type: object
summary: User login summary: User login
tags: tags:
- Authentication - Authentication

View File

@ -1,5 +0,0 @@
package models
type ErrorResponse struct {
Error string `json:"error"`
}

View File

@ -6,16 +6,9 @@ import "time"
// @Description Model of the Hostname as it // @Description Model of the Hostname as it
// @Description is represented in the database // @Description is represented in the database
type Hostname struct { type Hostname struct {
ID int `json:"Id" example:"25"` // Internal ID of the Hostname within the database ID int `json:"id"` // Internal ID of the Hostname within the database
Category string `json:"Category" example:"notebook"` // Category / Rule that was used when generating the hostname Category string `json:"category"` // Category / Rule that was used when generating the hostname
Hostname string `json:"Hostname" example:"ISEHENNB0009"` // Generated hostname Hostname string `json:"hostname"` // Generated hostname
Parameters map[string]interface{} `json:"Parameters"` // Parameter object of rule specific attributes, see rule.* models Parameters map[string]interface{} `json:"parameters"` // Parameter object of rule specific attributes
CreatedAt time.Time `json:"Created_at" example:"2024-01-16T12:53:59Z"` // Creation Time of the entry CreatedAt time.Time `json:"created_at"` // Creation Time of the entry
}
// SimpleHostnameResponse
// @Description Model of the Hostname as returned by
// @Description POST endpoint
type SimpleHostnameResponse struct {
Hostname string `json:"Hostname"` // Name of the newly generated host
} }

View File

@ -4,12 +4,6 @@ package models
// @Description User account information used in the login process // @Description User account information used in the login process
// @Description with Username and password // @Description with Username and password
type LoginCredentials struct { type LoginCredentials struct {
Username string `json:"Username"` Username string `json:"username"`
Password string `json:"Password"` Password string `json:"password"`
}
// JWT Token Response Model
// @Description Model returned after successful login
type TokenResponse struct {
Token string `json:"token"`
} }

View File

@ -32,7 +32,7 @@ func (br *BaseRule) baseInsert(category string, hostname string, paramsJSON []by
} }
func (br *BaseRule) baseUpdate(category string, oldhostname string, hostname string, paramsJSON []byte) error { func (br *BaseRule) baseUpdate(category string, oldhostname string, hostname string, paramsJSON []byte) error {
exists, err := db.HostnameExists(category, oldhostname) exists, err := db.HostnameExists(category, hostname)
if err != nil { if err != nil {
return fmt.Errorf("error checking existence of hostname: %v", err.Error()) return fmt.Errorf("error checking existence of hostname: %v", err.Error())
} }

View File

@ -73,7 +73,7 @@ func (nr *NotebookRule) Generate(params map[string]interface{}) (string, []byte,
// @Produce json // @Produce json
// @Tags Generating Hostnames // @Tags Generating Hostnames
// @Param body body NotebookRuleInput true "Input data to generate hostname" // @Param body body NotebookRuleInput true "Input data to generate hostname"
// @Success 200 {object} models.SimpleHostnameResponse "Hostname" // @Success 200 {string} string "Hostname"
// @Router /api/notebook [post] // @Router /api/notebook [post]
func (nr *NotebookRule) Insert(category string, hostname string, paramsJSON []byte) error { func (nr *NotebookRule) Insert(category string, hostname string, paramsJSON []byte) error {
return nr.baseInsert(category, hostname, paramsJSON) return nr.baseInsert(category, hostname, paramsJSON)
@ -86,7 +86,7 @@ func (nr *NotebookRule) Insert(category string, hostname string, paramsJSON []by
// @Produce json // @Produce json
// @Tags Generating Hostnames // @Tags Generating Hostnames
// @Param body body NotebookRuleInput true "Input data to generate hostname" // @Param body body NotebookRuleInput true "Input data to generate hostname"
// @Success 200 {object} models.SimpleHostnameResponse "Hostname" // @Success 200 {string} string "Hostname"
// @Router /api/notebook [put] // @Router /api/notebook [put]
func (nr *NotebookRule) Update(category string, oldhostname string, hostname string, paramsJSON []byte) error { func (nr *NotebookRule) Update(category string, oldhostname string, hostname string, paramsJSON []byte) error {
return nr.baseUpdate(category, oldhostname, hostname, paramsJSON) return nr.baseUpdate(category, oldhostname, hostname, paramsJSON)