| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/domains" |
| 9 | "github.com/carlosframework/platform/internal/hostclaim" |
| 10 | "github.com/carlosframework/platform/internal/hostops" |
| 11 | "github.com/carlosframework/platform/internal/provision" |
| 12 | ) |
| 13 | |
| 14 | func (s *Server) attachClaimFirstRoute(ctx context.Context, acct *Account, app, host, channel, principal string) (attachResult, *doorRefusal) { |
| 15 | if !s.domainProver(ctx, host, acct.Sqid)() { |
| 16 | return attachResult{}, &doorRefusal{http.StatusConflict, verifyGuidance(s.domainChallenge(host, acct.Sqid))} |
| 17 | } |
| 18 | request, err := s.beginClaimCreate(ctx, acct, app, host, principal, func(now time.Time, acquisition string) (hostops.Record, error) { |
| 19 | rec := provision.Record{Schema: 1, Host: host, App: app, Account: acct.Sqid, Channel: channel, Kind: "static", CreatedBy: principal, Acquisition: acquisition} |
| 20 | record, err := hostops.PrepareInstance(rec, now, s.cfg.claimPlacementRegion(rec.Region)) |
| 21 | record.VerifiedOwnership = true |
| 22 | return record, err |
| 23 | }) |
| 24 | if err != nil { |
| 25 | return attachResult{}, claimResponseRefusal(err) |
| 26 | } |
| 27 | defer request.Close() |
| 28 | err = request.Authority.RecordEffects(request.Context, request.Observed, request.Record) |
| 29 | if err == nil { |
| 30 | _, err = s.cfg.HostClaimOps.Complete(request.Context, host, request.Observed.Claim.Op.ID) |
| 31 | } |
| 32 | h := request.Observed.Claim.Holder |
| 33 | result := attachResult{Claim: domains.Claim{Schema: 1, Host: host, Account: acct.Sqid, App: app, Acquisition: h.Acquisition, RecordIncarnation: h.Incarnation, OwnershipState: "active"}, Created: true, FirstRoute: true, Channel: channel} |
| 34 | if err != nil { |
| 35 | fresh, readErr := hostclaim.Read(context.WithoutCancel(ctx), s.cfg.Releases, host) |
| 36 | if readErr != nil || fresh.Absent || fresh.Claim.Holder == nil || fresh.Claim.Holder.Incarnation != h.Incarnation || fresh.Claim.Op.ID != request.Observed.Claim.Op.ID { |
| 37 | return attachResult{}, claimResponseRefusal(hostops.ErrPending) |
| 38 | } |
| 39 | result.OperationID = fresh.Claim.Op.ID |
| 40 | result.OperationPhase = fresh.Claim.Op.Phase |
| 41 | if result.OperationPhase == "" { |
| 42 | result.OperationPhase = fresh.Claim.Op.Kind |
| 43 | } |
| 44 | } |
| 45 | s.bumpSerial(ctx) |
| 46 | return result, nil |
| 47 | } |
| 48 | |