-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from cloudgraphdev/feature/CG-1210
fet(services): Create Azure logProfiles service
- Loading branch information
Showing
15 changed files
with
258 additions
and
1 deletion.
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
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 { ServiceConnection } from '@cloudgraph/sdk' | ||
import { isEmpty } from 'lodash' | ||
|
||
import services from '../../enums/services' | ||
import { RawAzureStorageAccount } from '../storageAccount/data' | ||
import { RawAzureLogProfileResource } from './data' | ||
|
||
export default ({ | ||
service, | ||
data, | ||
region, | ||
}: { | ||
service: RawAzureLogProfileResource | ||
data: Array<{ name: string; data: { [property: string]: any[] } }> | ||
region: string | ||
}): { | ||
[property: string]: ServiceConnection[] | ||
} => { | ||
const connections: ServiceConnection[] = [] | ||
const { id, storageAccountId } = service | ||
|
||
/** | ||
* Find storage account related to this log profile | ||
*/ | ||
const storageAccounts: { | ||
name: string | ||
data: { [property: string]: RawAzureStorageAccount[] } | ||
} = data.find(({ name }) => name === services.storageAccount) | ||
|
||
if (storageAccounts?.data?.[region]) { | ||
const storageAccountsInRegion: RawAzureStorageAccount[] = | ||
storageAccounts.data[region].filter( | ||
({ id: saId }: RawAzureStorageAccount) => saId === storageAccountId | ||
) | ||
|
||
if (!isEmpty(storageAccountsInRegion)) { | ||
for (const rg of storageAccountsInRegion) { | ||
connections.push({ | ||
id: rg.id, | ||
resourceType: services.storageAccount, | ||
relation: 'child', | ||
field: 'storageAccount', | ||
}) | ||
} | ||
} | ||
} | ||
|
||
const rgResult = { | ||
[id]: connections, | ||
} | ||
return rgResult | ||
} |
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,65 @@ | ||
import CloudGraph from '@cloudgraph/sdk' | ||
import { MonitorClient, LogProfileResource } from '@azure/arm-monitor' | ||
import { PagedAsyncIterableIterator } from '@azure/core-paging' | ||
import azureLoggerText from '../../properties/logger' | ||
import { AzureServiceInput, TagMap } from '../../types' | ||
import { tryCatchWrapper } from '../../utils' | ||
import { regionMap } from '../../enums/regions' | ||
|
||
const { logger } = CloudGraph | ||
const lt = { ...azureLoggerText } | ||
const serviceName = 'Log Profiles' | ||
|
||
export interface RawAzureLogProfileResource | ||
extends Omit<LogProfileResource, 'location' | 'tags'> { | ||
region: string | ||
Tags: TagMap | ||
} | ||
|
||
export default async ({ | ||
config, | ||
}: AzureServiceInput): Promise<{ | ||
[property: string]: RawAzureLogProfileResource[] | ||
}> => { | ||
try { | ||
const { tokenCredentials, subscriptionId } = config | ||
const client = new MonitorClient(tokenCredentials, subscriptionId) | ||
const logProfiles: RawAzureLogProfileResource[] = [] | ||
const result = { global: [] } | ||
await tryCatchWrapper( | ||
async () => { | ||
const logProfilesIterable: PagedAsyncIterableIterator<LogProfileResource> = | ||
client.logProfiles.list() | ||
for await (const logProfile of logProfilesIterable) { | ||
if (logProfile) { | ||
const { tags, ...rest } = logProfile | ||
const region = regionMap.global | ||
logProfiles.push({ | ||
...rest, | ||
region, | ||
Tags: tags || {}, | ||
}) | ||
} | ||
} | ||
}, | ||
{ | ||
service: serviceName, | ||
client, | ||
scope: 'logProfiles', | ||
operation: 'list', | ||
} | ||
) | ||
logger.debug(lt.foundLogProfiles(logProfiles.length)) | ||
|
||
logProfiles.map(({ region, ...rest }) => { | ||
result.global.push({ | ||
...rest, | ||
region, | ||
}) | ||
}) | ||
return result | ||
} catch (e) { | ||
logger.error(e) | ||
return {} | ||
} | ||
} |
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,40 @@ | ||
import cuid from 'cuid' | ||
import { AzureLogProfile } from '../../types/generated' | ||
import { formatTagsFromMap } from '../../utils/format' | ||
import { RawAzureLogProfileResource } from './data' | ||
|
||
export default ({ | ||
service, | ||
account, | ||
region, | ||
}: { | ||
service: RawAzureLogProfileResource | ||
account: string | ||
region: string | ||
}): AzureLogProfile => { | ||
const { | ||
id, | ||
name, | ||
type, | ||
storageAccountId, | ||
serviceBusRuleId, | ||
locations = [], | ||
categories = [], | ||
retentionPolicy = {}, | ||
Tags: tags = {}, | ||
} = service | ||
|
||
return { | ||
id: id || cuid(), | ||
name, | ||
region, | ||
subscriptionId: account, | ||
type, | ||
storageAccountId, | ||
serviceBusRuleId, | ||
locations, | ||
categories, | ||
retentionPolicy, | ||
tags: formatTagsFromMap(tags), | ||
} | ||
} |
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,16 @@ | ||
import { Service } from '@cloudgraph/sdk' | ||
import BaseService from '../base' | ||
import getConnections from './connections' | ||
import format from './format' | ||
import mutation from './mutation' | ||
import getData from './data' | ||
|
||
export default class LogProfiles extends BaseService implements Service { | ||
format = format.bind(this) | ||
|
||
getConnections = getConnections.bind(this) | ||
|
||
getData = getData.bind(this) | ||
|
||
mutation = mutation | ||
} |
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,5 @@ | ||
export default `mutation($input: [AddazureLogProfileInput!]!) { | ||
addazureLogProfile(input: $input, upsert: true) { | ||
numUids | ||
} | ||
}`; |
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,26 @@ | ||
type azureLogProfileRetentionPolicy | ||
@generate( | ||
query: { get: false, query: true, aggregate: false } | ||
mutation: { add: false, delete: false } | ||
subscription: false | ||
) { | ||
enabled: Boolean @search | ||
days: Int @search | ||
} | ||
|
||
type azureLogProfile implements azureBaseResource | ||
@generate( | ||
query: { get: true, query: true, aggregate: true } | ||
mutation: { add: true, delete: false } | ||
) | ||
@key(fields: "id") { | ||
region: String @search(by: [hash, regexp]) | ||
subscriptionId: String @search(by: [hash, regexp]) | ||
storageAccountId: String @search(by: [hash, regexp]) | ||
serviceBusRuleId: String @search(by: [hash, regexp]) | ||
locations: [String] @search(by: [hash, regexp]) | ||
categories: [String] @search(by: [hash, regexp]) | ||
retentionPolicy: azureLogProfileRetentionPolicy | ||
tags: [azureRawTag] | ||
storageAccount: [azureStorageAccount] @hasInverse(field: logProfiles) | ||
} |
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