Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added rescan functionality #1347

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState, useEffect, useRef } from 'react';
import Avatar from '@components/avatar/Avatar';
import platform from '@utils/providerHelper';
import { RefreshIcon } from '@components/icons';
import settingsService from '@services/settingsService';
import { CloudAccount } from '../hooks/useCloudAccounts/useCloudAccount';
import CloudAccountStatus from './CloudAccountStatus';
import More2Icon from '../../icons/More2Icon';
Expand Down Expand Up @@ -41,6 +43,16 @@ export default function CloudAccountItem({
};
}, []);

const handleRescanClick = () => {
settingsService.rescanCloudAccount(id as number).then(res => {
if (res === Error) {
console.log('error', res);
} else {
window.location.reload();
}
});
};

return (
<div
key={id}
Expand Down Expand Up @@ -76,6 +88,20 @@ export default function CloudAccountItem({
<EditIcon className="mr-2 h-6 w-6" />
Edit cloud account
</button>
<button
className="flex w-full rounded-md py-3 pl-3 pr-5 text-left text-sm text-gray-700 hover:bg-background-ds"
onClick={() => {
handleRescanClick();
}}
disabled={status === 'SCANNING'}
style={{
opacity: status === 'SCANNING' ? 0.5 : 1,
pointerEvents: status === 'SCANNING' ? 'none' : 'auto'
}}
>
<RefreshIcon className="mr-2 h-6 w-6" />
Rescan
</button>
<button
className="flex w-full rounded-md py-3 pl-3 pr-5 text-left text-sm text-red-500 hover:bg-background-ds"
onClick={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ function CloudAccountStatus({ status }: { status: CloudAccount['status'] }) {
className={classNames(
'relative inline-block rounded-3xl px-2 py-1 text-sm',
{
'bg-green-200 text-green-600': status === 'CONNECTED',
'bg-green-200 text-green-600':
status === 'CONNECTED' || status === 'SCANNING',
'bg-red-200 text-red-600':
status === 'PERMISSION_ISSUE' || status === 'INTEGRATION_ISSUE'
status === 'PERMISSION ISSUE' || status === 'INTEGRATION ISSUE'
}
)}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface CloudAccount {
name: string;
provider: Provider;
resources?: number;
status?: 'CONNECTED' | 'INTEGRATION_ISSUE' | 'PERMISSION_ISSUE';
status?: 'CONNECTED' | 'INTEGRATION ISSUE' | 'PERMISSION ISSUE' | 'SCANNING';
}

export interface CloudAccountPayload<T extends Credentials> {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/components/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function Layout({ children }: LayoutProps) {
}
}, [telemetry]);

const betaFlagOnboardingWizard = false; // set this to true once wizard gets good support of the backend
const betaFlagOnboardingWizard = true; // set this to true once wizard gets good support of the backend
const isOnboarding =
betaFlagOnboardingWizard && router.pathname.startsWith('/onboarding');

Expand Down
13 changes: 13 additions & 0 deletions dashboard/services/settingsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,19 @@ const settingsService = {
}
},

async rescanCloudAccount(id: number) {
try {
const res = await fetch(
`${BASE_URL}/cloud_accounts/resync/${id}`,
settings('GET')
);
const data = await res.json();
return data;
} catch (error) {
return Error;
}
},

async saveDatabaseConfig(payload: string) {
try {
const res = await fetch(
Expand Down
17 changes: 17 additions & 0 deletions handlers/accounts_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ func (handler *ApiHandler) NewCloudAccountHandler(c *gin.Context) {
c.JSON(http.StatusCreated, account)
}

func (handler *ApiHandler) ReScanAccount(c *gin.Context) {
accountId := c.Param("id")

account := new(models.Account)
res, err := handler.db.NewUpdate().Model(account).Set("status = ? ", "SCANNING").Where("id = ?", accountId).Where("status = ?", "CONNECTED").Returning("*").Exec(handler.ctx)
if err != nil {
log.Error("Couldn't set status", err)
return
}
rows, _ := res.RowsAffected()
if rows > 0 {
go fetchResourcesForAccount(handler.ctx, *account, handler.db, []string{})
}

c.JSON(http.StatusOK, "Rescan Triggered")
}

func (handler *ApiHandler) DeleteCloudAccountHandler(c *gin.Context) {
accountId := c.Param("id")

Expand Down
20 changes: 19 additions & 1 deletion handlers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func triggerFetchingWorkflow(ctx context.Context, client providers.ProviderClien

func fetchResourcesForAccount(ctx context.Context, account models.Account, db *bun.DB, regions []string) {
numWorkers := 64
account.Status = ""
wp := providers.NewWorkerPool(numWorkers)
wp.Start()

Expand All @@ -110,9 +111,20 @@ func fetchResourcesForAccount(ctx context.Context, account models.Account, db *b

client, err := makeClientFromAccount(account)
if err != nil {
log.Error(err)
log.Error(err, account.Id, account.Provider)
_, err := db.NewUpdate().Model(&account).Set("status = ? ", "INTEGRATION ISSUE").Where("name = ?", account.Name).Exec(ctx)
if err != nil {
log.Error(err)
return
}
return
}
_, err = db.NewUpdate().Model(&account).Set("status = ? ", "SCANNING").Where("name = ?", account.Name).Exec(ctx)
if err != nil {
log.Error("Couldn't set status")
return
}
log.Info("Scanning status set")
if client.AWSClient != nil {
workflowTrigger(*client, "AWS")
} else if client.DigitalOceanClient != nil {
Expand Down Expand Up @@ -141,6 +153,12 @@ func fetchResourcesForAccount(ctx context.Context, account models.Account, db *b

wwg.Wait()
wp.Wait()
_, err = db.NewUpdate().Model(&account).Set("status = ? ", "CONNECTED").Where("name = ?", account.Name).Exec(ctx)
if err != nil {
log.Error("Couldn't set status")
return
}
log.Info("Scanning done")
}

func makeClientFromAccount(account models.Account) (*providers.ProviderClient, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/api/v1/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func Endpoints(ctx context.Context, telemetry bool, analytics utils.Analytics, d
router.POST("/cloud_accounts", api.NewCloudAccountHandler)
router.DELETE("/cloud_accounts/:id", api.DeleteCloudAccountHandler)
router.PUT("/cloud_accounts/:id", api.UpdateCloudAccountHandler)
router.GET("/cloud_accounts/resync/:id", api.ReScanAccount)

router.POST("/databases", api.ConfigureDatabaseHandler)

Expand Down
Loading