-
Notifications
You must be signed in to change notification settings - Fork 239
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
Feroze Mohideen
authored
May 8, 2024
1 parent
7139832
commit 8e442ec
Showing
34 changed files
with
1,062 additions
and
125 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
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,69 @@ | ||
package neon_integration | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/porter-dev/porter/api/server/handlers" | ||
"github.com/porter-dev/porter/api/server/shared" | ||
"github.com/porter-dev/porter/api/server/shared/apierrors" | ||
"github.com/porter-dev/porter/api/server/shared/config" | ||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/models" | ||
"github.com/porter-dev/porter/internal/telemetry" | ||
) | ||
|
||
// ListNeonIntegrationsHandler is a struct for listing all noen integrations for a given project | ||
type ListNeonIntegrationsHandler struct { | ||
handlers.PorterHandlerReadWriter | ||
} | ||
|
||
// NewListNeonIntegrationsHandler constructs a ListNeonIntegrationsHandler | ||
func NewListNeonIntegrationsHandler( | ||
config *config.Config, | ||
decoderValidator shared.RequestDecoderValidator, | ||
writer shared.ResultWriter, | ||
) *ListNeonIntegrationsHandler { | ||
return &ListNeonIntegrationsHandler{ | ||
PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), | ||
} | ||
} | ||
|
||
// NeonIntegration describes a neon integration | ||
type NeonIntegration struct { | ||
CreatedAt time.Time `json:"created_at"` | ||
} | ||
|
||
// ListNeonIntegrationsResponse describes the list neon integrations response body | ||
type ListNeonIntegrationsResponse struct { | ||
// Integrations is a list of neon integrations | ||
Integrations []NeonIntegration `json:"integrations"` | ||
} | ||
|
||
// ServeHTTP returns a list of neon integrations associated with the specified project | ||
func (h *ListNeonIntegrationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, span := telemetry.NewSpan(r.Context(), "serve-list-neon-integrations") | ||
defer span.End() | ||
|
||
project, _ := ctx.Value(types.ProjectScope).(*models.Project) | ||
|
||
resp := ListNeonIntegrationsResponse{} | ||
integrationList := make([]NeonIntegration, 0) | ||
|
||
integrations, err := h.Repo().NeonIntegration().Integrations(ctx, project.ID) | ||
if err != nil { | ||
err := telemetry.Error(ctx, span, err, "error getting datastores") | ||
h.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) | ||
return | ||
} | ||
|
||
for _, int := range integrations { | ||
integrationList = append(integrationList, NeonIntegration{ | ||
CreatedAt: int.CreatedAt, | ||
}) | ||
} | ||
|
||
resp.Integrations = integrationList | ||
|
||
h.WriteResult(w, r, resp) | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,52 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
/** | ||
* Hook to open an authentication window at a given url. | ||
* Once the auth flow redirects back to Porter, the window is closed. | ||
*/ | ||
export const useAuthWindow = ({ | ||
authUrl, | ||
}: { | ||
authUrl: string; | ||
}): { | ||
openAuthWindow: () => void; | ||
} => { | ||
const [authWindow, setAuthWindow] = useState<Window | null>(null); | ||
|
||
const openAuthWindow = (): void => { | ||
const windowObjectReference = window.open( | ||
authUrl, | ||
"porterAuthWindow", | ||
"width=600,height=700,left=200,top=200" | ||
); | ||
setAuthWindow(windowObjectReference); | ||
}; | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
if (authWindow) { | ||
try { | ||
if ( | ||
authWindow.location.hostname.includes("dashboard.getporter.dev") || | ||
authWindow.location.hostname.includes("localhost") | ||
) { | ||
authWindow.close(); | ||
setAuthWindow(null); | ||
clearInterval(interval); | ||
} | ||
} catch (e) { | ||
console.log("Error accessing the authentication window.", e); | ||
} | ||
} | ||
}, 1000); | ||
|
||
return () => { | ||
clearInterval(interval); | ||
if (authWindow) { | ||
authWindow.close(); | ||
} | ||
}; | ||
}, [authWindow]); | ||
|
||
return { openAuthWindow }; | ||
}; |
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,41 @@ | ||
import { z } from "zod"; | ||
|
||
import { | ||
neonIntegrationValidator, | ||
type ClientNeonIntegration, | ||
} from "lib/neon/types"; | ||
|
||
import api from "shared/api"; | ||
|
||
type TUseNeon = { | ||
getNeonIntegrations: ({ | ||
projectId, | ||
}: { | ||
projectId: number; | ||
}) => Promise<ClientNeonIntegration[]>; | ||
}; | ||
export const useNeon = (): TUseNeon => { | ||
const getNeonIntegrations = async ({ | ||
projectId, | ||
}: { | ||
projectId: number; | ||
}): Promise<ClientNeonIntegration[]> => { | ||
const response = await api.getNeonIntegrations( | ||
"<token>", | ||
{}, | ||
{ | ||
projectId, | ||
} | ||
); | ||
|
||
const results = await z | ||
.object({ integrations: z.array(neonIntegrationValidator) }) | ||
.parseAsync(response.data); | ||
|
||
return results.integrations; | ||
}; | ||
|
||
return { | ||
getNeonIntegrations, | ||
}; | ||
}; |
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,6 @@ | ||
import { z } from "zod"; | ||
|
||
export const neonIntegrationValidator = z.object({ | ||
created_at: z.string(), | ||
}); | ||
export type ClientNeonIntegration = z.infer<typeof neonIntegrationValidator>; |
Oops, something went wrong.