rohankanojia

Untitled

Jun 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.77 KB | None | 0 0
  1. diff --git a/internal/jenkinsapi/jenkinsapi.go b/internal/jenkinsapi/jenkinsapi.go
  2. index 70ad8c5..51ff2c6 100644
  3. --- a/internal/jenkinsapi/jenkinsapi.go
  4. +++ b/internal/jenkinsapi/jenkinsapi.go
  5. @@ -15,6 +15,7 @@ import (
  6.  //JenkinsAPI contains API to check whether Jenkins for the current user is idle|running|starting
  7.  type JenkinsAPI interface {
  8.         Start(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
  9. +        Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
  10.  }
  11.  
  12.  // JenkinsAPIImpl implements JenkinsAPI
  13. @@ -31,6 +32,34 @@ func NewJenkinsAPI(tenant clients.TenantService, idler clients.IdlerService) Jen
  14.         }
  15.  }
  16.  
  17. +// Returns the Jenkins pods status for current user
  18. +func (api *jenkinsAPIImpl) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  19. +    resp := clients.StatusResponse{}
  20. +
  21. +    authHeader := r.Header.Get("Authorization")
  22. +    if !strings.HasPrefix(authHeader, "Bearer ") {
  23. +        HandleError(w, resp, errors.New("Could not find Bearer token in Authorization Header"), http.StatusUnauthorized)
  24. +        return
  25. +    }
  26. +    accessToken := strings.Split(r.Header.Get("Authorization"), " ")[1]
  27. +    namespace, err := api.tenant.GetNamespace(accessToken)
  28. +    if err != nil {
  29. +            HandleError(w, resp, err, http.StatusUnauthorized)
  30. +            return
  31. +    }
  32. +    log.Infof("Found token info in the query. Namespace is %s and clusterURL is %s", namespace.Name, namespace.ClusterURL)
  33. +
  34. +    status, err := api.idler.State(namespace.Name, namespace.ClusterURL)
  35. +    if err != nil {
  36. +        HandleError(w, resp, err, http.StatusInternalServerError)
  37. +        return
  38. +    }
  39. +    resp.Data = &clients.JenkinsInfo{
  40. +        State: status,
  41. +    }
  42. +    json.NewEncoder(w).Encode(resp)
  43. +}
  44. +
  45.  // Start returns the Jenkins status for the current user
  46.  func (api *jenkinsAPIImpl) Start(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  47.         resp := clients.StatusResponse{}
  48. diff --git a/internal/router/router.go b/internal/router/router.go
  49. index cbf9775..cec53c3 100644
  50. --- a/internal/router/router.go
  51. +++ b/internal/router/router.go
  52. @@ -27,6 +27,14 @@ func CreateJenkinsAPIRouter(jenkinsAPI jenkinsapi.JenkinsAPI) *httprouter.Router
  53.         return jenkinsAPIRouter
  54.  }
  55.  
  56. +// GetJenkinsStatus is getting status of jenkins pods
  57. +func GetJenkinsPodsStatus(jenkinsAPI jenkinsapi.JenkinsAPI) *httprouter.Router {
  58. +    // Create router for API
  59. +    jenkinsAPIRouter := httprouter.New()
  60. +    jenkinsAPIRouter.GET("/api/jenkins/status", jenkinsAPI.Status);
  61. +    return jenkinsAPIRouter
  62. +}
  63. +
  64.  // CreateProxyRouter is the HTTP server handler which handles the incoming webhook and UI requests.
  65.  func CreateProxyRouter(proxy *proxy.Proxy) *http.ServeMux {
  66.         proxyMux := http.NewServeMux()
Add Comment
Please, Sign In to add comment