-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51d2cf5
commit 9cb4888
Showing
5 changed files
with
256 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/gorilla/mux" | ||
"github.com/spectrocloud/terraform-provider-spectrocloud/tests/mockApiServer/routes" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// API key for authentication | ||
const apiKey = "12345" | ||
|
||
// Aggregate all routes into slices for different servers | ||
var allRoutesPositive []routes.Route | ||
var allRoutesNegative []routes.Route | ||
|
||
// Middleware to check for the API key and log the Project-ID if present | ||
func apiKeyAuthMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Header.Get("ApiKey") != apiKey { | ||
http.Error(w, "Forbidden", http.StatusForbidden) | ||
return | ||
} | ||
// Log the Project-ID if it is present | ||
if projectID := r.Header.Get("Project-ID"); projectID != "" { | ||
log.Printf("Project-ID: %s", projectID) | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func main() { | ||
// Create routers for different ports | ||
router8080 := mux.NewRouter() | ||
router8888 := mux.NewRouter() | ||
|
||
// Set up routes for port 8080 | ||
setupRoutes(router8080, allRoutesPositive) | ||
|
||
// Set up routes for port 8888 | ||
setupRoutes(router8888, allRoutesNegative) | ||
|
||
// Start servers on different ports | ||
go func() { | ||
log.Println("Starting server on :8080...") | ||
if err := http.ListenAndServeTLS(":8080", "mock_server.crt", "mock_server.key", router8080); err != nil { | ||
log.Fatalf("Server failed to start on port 8080: %v", err) | ||
} | ||
}() | ||
|
||
log.Println("Starting server on :8888...") | ||
|
||
if err := http.ListenAndServeTLS(":8888", "mock_server.crt", "mock_server.key", router8888); err != nil { | ||
log.Fatalf("Server failed to start on port 8080: %v", err) | ||
} | ||
} | ||
|
||
// setupRoutes configures the given router with the provided routes | ||
func setupRoutes(router *mux.Router, routes []routes.Route) { | ||
// Apply API key middleware to all routes | ||
router.Use(apiKeyAuthMiddleware) | ||
|
||
// Register all routes | ||
for _, route := range routes { | ||
route := route // capture the range variable | ||
|
||
router.HandleFunc(route.Path, func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(route.Response.StatusCode) | ||
if route.Response.Payload != nil { | ||
err := json.NewEncoder(w).Encode(route.Response.Payload) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
}).Methods(route.Method) | ||
} | ||
} | ||
|
||
func init() { | ||
// Initialize routes for port 8080 | ||
allRoutesPositive = append(allRoutesPositive, routes.ProjectRoutes()...) | ||
allRoutesPositive = append(allRoutesPositive, routes.CommonProjectRoutes()...) | ||
|
||
// Initialize routes for port 8888 | ||
allRoutesNegative = append(allRoutesNegative, routes.ProjectNegativeRoutes()...) | ||
allRoutesNegative = append(allRoutesNegative, routes.CommonProjectRoutes()...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package routes | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/hex" | ||
"github.com/spectrocloud/palette-sdk-go/api/models" | ||
"net/http" | ||
) | ||
|
||
// ResponseData defines the structure of mock responses | ||
type ResponseData struct { | ||
StatusCode int | ||
Payload interface{} | ||
} | ||
|
||
// Route defines a mock route with method, path, and response | ||
type Route struct { | ||
Method string | ||
Path string | ||
Response ResponseData | ||
} | ||
|
||
func generateRandomStringUID() string { | ||
bytes := make([]byte, 24/2) | ||
_, err := rand.Read(bytes) | ||
if err != nil { | ||
return "test" | ||
} | ||
return hex.EncodeToString(bytes) | ||
} | ||
|
||
func CommonProjectRoutes() []Route { | ||
return []Route{ | ||
{ | ||
Method: "GET", | ||
Path: "/v1/health", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusOK, | ||
Payload: map[string]interface{}{ | ||
"healthy": true, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func getError(code string, msg string) models.V1Error { | ||
return models.V1Error{ | ||
Code: code, | ||
Details: nil, | ||
Message: msg, | ||
Ref: "ref-" + generateRandomStringUID(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/spectrocloud/palette-sdk-go/api/models" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
func getMockProjectPayload() models.V1Project { | ||
return models.V1Project{ | ||
Metadata: &models.V1ObjectMeta{ | ||
Annotations: nil, | ||
CreationTimestamp: models.V1Time{}, | ||
DeletionTimestamp: models.V1Time{}, | ||
Labels: map[string]string{ | ||
"description": "default project", | ||
}, | ||
LastModifiedTimestamp: models.V1Time{}, | ||
Name: "Default", | ||
UID: generateRandomStringUID(), | ||
}, | ||
Spec: &models.V1ProjectSpec{ | ||
Alerts: nil, | ||
LogoURL: "", | ||
Teams: nil, | ||
Users: nil, | ||
}, | ||
Status: &models.V1ProjectStatus{ | ||
CleanUpStatus: nil, | ||
IsDisabled: false, | ||
}, | ||
} | ||
|
||
} | ||
|
||
func ProjectRoutes() []Route { | ||
return []Route{ | ||
{ | ||
Method: "POST", | ||
Path: "/v1/projects", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusCreated, | ||
Payload: map[string]interface{}{"UID": generateRandomStringUID()}, | ||
}, | ||
}, | ||
{ | ||
Method: "GET", | ||
Path: "/v1/projects/{uid}", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusOK, | ||
Payload: getMockProjectPayload(), | ||
}, | ||
}, | ||
{ | ||
Method: "PUT", | ||
Path: "/v1/projects/{uid}", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusNoContent, | ||
Payload: map[string]interface{}{"UID": generateRandomStringUID()}, | ||
}, | ||
}, | ||
{ | ||
Method: "DELETE", | ||
Path: "/v1/projects/{uid}", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusNoContent, | ||
Payload: nil, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ProjectNegativeRoutes() []Route { | ||
return []Route{ | ||
{ | ||
Method: "POST", | ||
Path: "/v1/projects", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusConflict, | ||
Payload: getError(strconv.Itoa(http.StatusConflict), "Project already exist"), | ||
}, | ||
}, | ||
{ | ||
Method: "GET", | ||
Path: "/v1/projects/{uid}", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusNotFound, | ||
Payload: getError(strconv.Itoa(http.StatusOK), "Project not found"), | ||
}, | ||
}, | ||
{ | ||
Method: "PUT", | ||
Path: "/v1/projects/{uid}", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusMethodNotAllowed, | ||
Payload: getError(strconv.Itoa(http.StatusNoContent), "Operation not allowed"), | ||
}, | ||
}, | ||
{ | ||
Method: "DELETE", | ||
Path: "/v1/projects/{uid}", | ||
Response: ResponseData{ | ||
StatusCode: http.StatusNotFound, | ||
Payload: getError(strconv.Itoa(http.StatusOK), "Project not found"), | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters