Skip to content

Commit

Permalink
Create func: detectStorageAccount.
Browse files Browse the repository at this point in the history
  • Loading branch information
rujche committed Feb 18, 2025
1 parent a0da8ed commit 3386529
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
39 changes: 38 additions & 1 deletion analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func analyzePomProject(projectRootPath string, pomFileAbsolutePath string) (Proj
if err = detectEventHubs(&result, applicationName, pom, properties); err != nil {
return ProjectAnalysisResult{}, err
}
if err = detectStorageAccount(&result, applicationName, pom, properties); err != nil {
return ProjectAnalysisResult{}, err
}
return result, nil
}

Expand All @@ -122,7 +125,6 @@ func detectPostgresql(result *ProjectAnalysisResult, applicationName string, pom
properties map[string]string) error {
if hasDependency(pom, "org.postgresql", "postgresql") ||
hasDependency(pom, "com.azure.spring", "spring-cloud-azure-starter-jdbc-postgresql") {

return addApplicationRelatedBackingServiceToResult(result, applicationName, DefaultPostgresqlServiceName,
AzureDatabaseForPostgresql{getDatabaseNameFromSpringDataSourceUrlProperty(properties)})
}
Expand Down Expand Up @@ -211,12 +213,37 @@ func detectEventHubs(result *ProjectAnalysisResult, applicationName string, pom
eventHubsNamePropertyMap[propertyName] = propertyValue
}
}
// todo: avoid duplicated values
hubs = append(hubs, internal.DistinctValues(eventHubsNamePropertyMap)...)
}
return addApplicationRelatedBackingServiceToResult(result, applicationName, DefaultEventHubsServiceName,
AzureEventHubs{Hubs: hubs})
}

func detectStorageAccount(result *ProjectAnalysisResult, applicationName string, pom internal.Pom,
properties map[string]string) error {
if !hasDependency(pom, "com.azure.spring", "spring-cloud-azure-stream-binder-eventhubs") &&
!hasDependency(pom, "com.azure.spring", "spring-cloud-azure-starter-integration-eventhubs") &&
!hasDependency(pom, "com.azure.spring", "spring-messaging-azure-eventhubs") {
return nil
}
var containers []string
if (hasDependency(pom, "com.azure.spring", "spring-cloud-azure-stream-binder-eventhubs") &&
containsInKeywordInBindingName(properties)) ||
hasDependency(pom, "com.azure.spring", "spring-cloud-azure-starter-integration-eventhubs") ||
hasDependency(pom, "com.azure.spring", "spring-messaging-azure-eventhubs") {
containerNamePropertyMap := make(map[string]string)
for key, value := range properties {
if strings.HasSuffix(key, "spring.cloud.azure.eventhubs.processor.checkpoint-store.container-name") {
containerNamePropertyMap[key] = value
}
}
containers = append(containers, internal.DistinctValues(containerNamePropertyMap)...)
}
return addApplicationRelatedBackingServiceToResult(result, applicationName, DefaultStorageServiceName,
AzureStorageAccount{Containers: containers})
}

func hasDependency(pom internal.Pom, groupId string, artifactId string) bool {
for _, dep := range pom.Dependencies {
if dep.GroupId == groupId && dep.ArtifactId == artifactId {
Expand All @@ -234,3 +261,13 @@ func getDatabaseNameFromSpringDataSourceUrlProperty(properties map[string]string
}
return databaseName
}

func containsInKeywordInBindingName(properties map[string]string) bool {
bindingDestinations := internal.GetBindingDestinationMap(properties)
for bindingName := range bindingDestinations {
if strings.Contains(bindingName, "-in-") { // Example: consume-in-0
return true
}
}
return false
}
6 changes: 6 additions & 0 deletions analyzer/project_analysis_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ type AzureEventHubs struct {
Hubs []string
}

const DefaultStorageServiceName = "storage"

type AzureStorageAccount struct {
Containers []string
}

func addApplicationToResult(result *ProjectAnalysisResult, applicationName string, application Application) error {
if _, ok := result.Applications[applicationName]; ok {
return fmt.Errorf("applicationName %s already exists", applicationName)
Expand Down

0 comments on commit 3386529

Please sign in to comment.