Explorar el Código

populate digitalocean metadata

Alexander Belanger hace 4 años
padre
commit
6753eddf72

+ 2 - 0
api/server/handlers/oauth_callback/digitalocean.go

@@ -71,6 +71,8 @@ func (p *OAuthCallbackDOHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 		ProjectID: projID,
 	}
 
+	oauthInt.PopulateTargetMetadata()
+
 	// create the oauth integration first
 	oauthInt, err = p.Repo().OAuthIntegration().CreateOAuthIntegration(oauthInt)
 

+ 8 - 0
api/types/project_integration.go

@@ -26,6 +26,14 @@ type OAuthIntegration struct {
 
 	// The project that this integration belongs to
 	ProjectID uint `json:"project_id"`
+
+	// (optional) an identifying email on the target identity provider.
+	// for example, for DigitalOcean this is the user's email.
+	TargetEmail string `json:"target_email,omitempty"`
+
+	// (optional) an identifying string on the target identity provider.
+	// for example, for DigitalOcean this is the target project name.
+	TargetName string `json:"target_id,omitempty"`
 }
 
 type ListOAuthResponse []*OAuthIntegration

+ 36 - 5
internal/models/integrations/oauth.go

@@ -1,8 +1,10 @@
 package integrations
 
 import (
+	"context"
 	"time"
 
+	"github.com/digitalocean/godo"
 	"github.com/porter-dev/porter/api/types"
 	"gorm.io/gorm"
 )
@@ -38,11 +40,38 @@ type OAuthIntegration struct {
 	// The project that this integration belongs to
 	ProjectID uint `json:"project_id"`
 
+	// (optional) an identifying email on the target identity provider.
+	// for example, for DigitalOcean this is the user's email.
+	TargetEmail string `json:"target_email"`
+
+	// (optional) an identifying string on the target identity provider.
+	// for example, for DigitalOcean this is the target project name.
+	TargetName string `json:"target_id"`
+
 	// ------------------------------------------------------------------
 	// All fields encrypted before storage.
 	// ------------------------------------------------------------------
 }
 
+func (g *OAuthIntegration) PopulateTargetMetadata() {
+	switch g.Client {
+	case types.OAuthDigitalOcean:
+		client := godo.NewFromToken(string(g.AccessToken))
+
+		account, _, err := client.Account.Get(context.TODO())
+
+		if err == nil && account != nil {
+			g.TargetEmail = account.Email
+		}
+
+		proj, _, err := client.Projects.GetDefault(context.TODO())
+
+		if err == nil && proj != nil {
+			g.TargetName = proj.Name
+		}
+	}
+}
+
 // GithubAppOAuthIntegration is the model used for storing github app oauth data
 // Unlike the above, this model is tied to a specific user, not a project
 type GithubAppOAuthIntegration struct {
@@ -56,10 +85,12 @@ type GithubAppOAuthIntegration struct {
 // ToOAuthIntegrationType generates an external OAuthIntegration to be shared over REST
 func (o *OAuthIntegration) ToOAuthIntegrationType() *types.OAuthIntegration {
 	return &types.OAuthIntegration{
-		CreatedAt: o.CreatedAt,
-		ID:        o.ID,
-		Client:    o.Client,
-		UserID:    o.UserID,
-		ProjectID: o.ProjectID,
+		CreatedAt:   o.CreatedAt,
+		ID:          o.ID,
+		Client:      o.Client,
+		UserID:      o.UserID,
+		ProjectID:   o.ProjectID,
+		TargetEmail: o.TargetEmail,
+		TargetName:  o.TargetName,
 	}
 }