Skip to content

Commit

Permalink
Merge branch 'main' into sdcHostWin
Browse files Browse the repository at this point in the history
  • Loading branch information
Krunal-Thakkar committed Aug 16, 2024
2 parents 7ad7c8c + 13bcfb4 commit bc0d44b
Show file tree
Hide file tree
Showing 12 changed files with 796 additions and 2 deletions.
139 changes: 139 additions & 0 deletions docs/data-sources/os_repository.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
# Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved.
#
# Licensed under the Mozilla Public License Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://mozilla.org/MPL/2.0/
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

title: "powerflex_os_repository data source"
linkTitle: "powerflex_os_repository"
page_title: "powerflex_os_repository Data Source - powerflex"
subcategory: ""
description: |-
This datasource is used to query the existing OS Repository from the PowerFlex array. The information fetched from this datasource can be used for getting the details / for further processing in resource block.
---

# powerflex_os_repository (Data Source)

This datasource is used to query the existing OS Repository from the PowerFlex array. The information fetched from this datasource can be used for getting the details / for further processing in resource block.

## Example Usage

```terraform
/*
Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved.
Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://mozilla.org/MPL/2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
# commands to run this tf file : terraform init && terraform apply --auto-approve
# Get all OS Repositories
data "powerflex_os_repository" "example1" {
}
# Get OS Repository details by ID
data "powerflex_os_repository" "example2" {
# this datasource supports filters like os repsoitory ids, names.
# Note: If both filters are used simultaneously, the results will include any records that match either of the filters.
filter {
os_repo_ids = ["1234","5678"]
}
}
# Get OS Repository details by Name
data "powerflex_os_repository" "example3" {
# this datasource supports filters like os repsoitory ids, names.
# Note: If both filters are used simultaneously, the results will include any records that match either of the filters.
filter {
os_repo_names = ["test"]
}
}
output "os_repository_result" {
value = data.powerflex_os_repository.example1
}
```

After the successful execution of above said block, we can see the output by executing `terraform output` command. Also, we can fetch information via the variable: `data.powerflex_os_repository.datasource_block_name.attribute_name` where datasource_block_name is the name of the data source block and attribute_name is the attribute which user wants to fetch.

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `filter` (Block, Optional) (see [below for nested schema](#nestedblock--filter))

### Read-Only

- `id` (String) ID of the OS Repository Datasource
- `os_repositories` (Attributes List) List of OS Repository Models (see [below for nested schema](#nestedatt--os_repositories))

<a id="nestedblock--filter"></a>
### Nested Schema for `filter`

Optional:

- `os_repo_ids` (Set of String) List of OS Repository Ids.
- `os_repo_names` (Set of String) List of OS Repository names.


<a id="nestedatt--os_repositories"></a>
### Nested Schema for `os_repositories`

Read-Only:

- `base_url` (String) Base URL of the OS repository
- `created_by` (String) User who created the OS repository
- `created_date` (String) Date of creation of the OS Repository
- `from_web` (Boolean) Whether the OS repository is from the web or not
- `id` (String) ID of the OS Repository
- `image_type` (String) Type of the OS image. Supported types are redhat7, vmware_esxi, sles, windows2016, windows2019
- `in_use` (Boolean) Whether the OS repository is in use or not
- `metadata` (Attributes) Metadata of the OS Repository (see [below for nested schema](#nestedatt--os_repositories--metadata))
- `name` (String) Name of the OS repository
- `password` (String) Password of the OS repository
- `razor_name` (String) Name of the Razor
- `rcm_path` (String) Path of the RCM
- `repo_type` (String) Type of the OS repository. Default is ISO
- `source_path` (String) Source path of the OS image
- `state` (String) State of the OS repository
- `username` (String) Username of the OS repository

<a id="nestedatt--os_repositories--metadata"></a>
### Nested Schema for `os_repositories.metadata`

Read-Only:

- `repos` (Attributes List) List of OS Repository Metadata Repos (see [below for nested schema](#nestedatt--os_repositories--metadata--repos))

<a id="nestedatt--os_repositories--metadata--repos"></a>
### Nested Schema for `os_repositories.metadata.repos`

Read-Only:

- `base_path` (String) Base path of the OS Repository Metadata Repo
- `description` (String) Description of the OS Repository Metadata Repo
- `gpg_key` (String) GPG key of the OS Repository Metadata Repo
- `name` (String) Name of the OS Repository Metadata Repo
- `os_packages` (Boolean) Whether the OS Repository Metadata Repo has OS packages or not

43 changes: 43 additions & 0 deletions examples/data-sources/powerflex_os_repository/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved.
Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://mozilla.org/MPL/2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

# commands to run this tf file : terraform init && terraform apply --auto-approve

# Get all OS Repositories
data "powerflex_os_repository" "example1" {
}

# Get OS Repository details by ID
data "powerflex_os_repository" "example2" {
# this datasource supports filters like os repsoitory ids, names.
# Note: If both filters are used simultaneously, the results will include any records that match either of the filters.
filter {
os_repo_ids = ["1234","5678"]
}
}

# Get OS Repository details by Name
data "powerflex_os_repository" "example3" {
# this datasource supports filters like os repsoitory ids, names.
# Note: If both filters are used simultaneously, the results will include any records that match either of the filters.
filter {
os_repo_names = ["test"]
}
}
output "os_repository_result" {
value = data.powerflex_os_repository.example1
}
81 changes: 81 additions & 0 deletions powerflex/helper/os_repository_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/dell/goscaleio"
scaleiotypes "github.com/dell/goscaleio/types/v1"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

// GetOsRepositoryID gets the OS repository ID
Expand Down Expand Up @@ -66,6 +67,48 @@ func GetOSRepositoryByID(client *goscaleio.System, id string) (*scaleiotypes.OSR
return osRepo, nil
}

// GetAllOsRepositories gets all OS repositories
func GetAllOsRepositories(client *goscaleio.System) ([]scaleiotypes.OSRepository, error) {
osRepos, err := client.GetAllOSRepositories()
if err != nil {
return nil, fmt.Errorf("Error getting OS repositories")
}
// Filter ISO repos
var filteredOSRepos []scaleiotypes.OSRepository
for _, osRepo := range osRepos {
if osRepo.RepoType == "ISO" {
filteredOSRepos = append(filteredOSRepos, osRepo)
}
}
return filteredOSRepos, nil
}

// GetOSRepositoriesByNames gets the OS Repositories filtered by names
func GetOSRepositoriesByNames(client *goscaleio.System, names []basetypes.StringValue) ([]scaleiotypes.OSRepository, error) {
// Get all OS repositories from the client
osRepos, err := client.GetAllOSRepositories()
if err != nil {
return nil, fmt.Errorf("error getting all OS repositories: %w", err)
}

var filteredRepos []scaleiotypes.OSRepository
nameSet := make(map[string]struct{})

// create name set
for _, name := range names {
nameSet[name.ValueString()] = struct{}{}
}

// Filter the repositories based on the names provided
for _, osRepo := range osRepos {
if _, exists := nameSet[osRepo.Name]; exists {
filteredRepos = append(filteredRepos, osRepo)
}
}

return filteredRepos, nil
}

// UpdateOsRepositoryState updates the OS repository state
func UpdateOsRepositoryState(osRepo *scaleiotypes.OSRepository, plan models.OSRepositoryResource) models.OSRepositoryResource {
state := plan
Expand All @@ -86,3 +129,41 @@ func UpdateOsRepositoryState(osRepo *scaleiotypes.OSRepository, plan models.OSRe
state.State = types.StringValue(osRepo.State)
return state
}

// GetAllOSRepositoryState sets the state for the OS repository datasource.
func GetAllOSRepositoryState(input scaleiotypes.OSRepository) models.OSRepositoryModel {
return models.OSRepositoryModel{
ID: types.StringValue(input.ID),
Name: types.StringValue(input.Name),
RCMPath: types.StringValue(input.RCMPath),
BaseURL: types.StringValue(input.BaseURL),
CreatedBy: types.StringValue(input.CreatedBy),
CreatedDate: types.StringValue(input.CreatedDate),
FromWeb: types.BoolValue(input.FromWeb),
RazorName: types.StringValue(input.RazorName),
ImageType: types.StringValue(input.ImageType),
InUse: types.BoolValue(input.InUse),
UserName: types.StringValue(input.UserName),
Password: types.StringValue(input.Password),
RepoType: types.StringValue(input.RepoType),
SourcePath: types.StringValue(input.SourcePath),
State: types.StringValue(input.State),
Metadata: UpdateOsRepoMetadata(input.Metadata),
}
}

// UpdateOsRepoMetadata sets the state for osRepoMetadata
func UpdateOsRepoMetadata(osRepoMetadata scaleiotypes.OSRepositoryMetadata) models.OSRepositoryMetadataModel {
state := models.OSRepositoryMetadataModel{}
for _, repo := range osRepoMetadata.Repos {
repoModel := models.OSRepositoryMetadataRepoModel{
BasePath: types.StringValue(repo.BasePath),
Description: types.StringValue(repo.Description),
GPGKey: types.StringValue(repo.GPGKey),
Name: types.StringValue(repo.Name),
OSPackages: types.BoolValue(repo.OSPackages),
}
state.Repos = append(state.Repos, repoModel)
}
return state
}
48 changes: 48 additions & 0 deletions powerflex/models/os_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,51 @@ type OSRepositoryResource struct {
FromWeb types.Bool `tfsdk:"from_web"`
Timeout types.Int64 `tfsdk:"timeout"`
}

// OSRepositoryDataSource defines the model for Os Repository Datasource
type OSRepositoryDataSource struct {
ID types.String `tfsdk:"id"`
OSRepositories []OSRepositoryModel `tfsdk:"os_repositories"`
// filter
OSRepoFilter *OSRepoFilter `tfsdk:"filter"`
}

// OSRepoFilter defines the model for filters used for OSRepositoryDataSource
type OSRepoFilter struct {
Names []types.String `tfsdk:"os_repo_names"`
IDs []types.String `tfsdk:"os_repo_ids"`
}

// OSRepositoryModel is the model for OS Repository Model
type OSRepositoryModel struct {
ID types.String `tfsdk:"id"`
CreatedDate types.String `tfsdk:"created_date"`
ImageType types.String `tfsdk:"image_type"`
SourcePath types.String `tfsdk:"source_path"`
RazorName types.String `tfsdk:"razor_name"`
InUse types.Bool `tfsdk:"in_use"`
UserName types.String `tfsdk:"username"`
CreatedBy types.String `tfsdk:"created_by"`
Password types.String `tfsdk:"password"`
Name types.String `tfsdk:"name"`
State types.String `tfsdk:"state"`
RepoType types.String `tfsdk:"repo_type"`
RCMPath types.String `tfsdk:"rcm_path"`
BaseURL types.String `tfsdk:"base_url"`
FromWeb types.Bool `tfsdk:"from_web"`
Metadata OSRepositoryMetadataModel `tfsdk:"metadata"`
}

// OSRepositoryMetadataModel is the model for OS Repository Metadata
type OSRepositoryMetadataModel struct {
Repos []OSRepositoryMetadataRepoModel `tfsdk:"repos"`
}

// OSRepositoryMetadataRepoModel is the model for OSRepositoryMetadataRepo
type OSRepositoryMetadataRepoModel struct {
BasePath types.String `tfsdk:"base_path"`
Description types.String `tfsdk:"description"`
GPGKey types.String `tfsdk:"gpg_key"`
Name types.String `tfsdk:"name"`
OSPackages types.Bool `tfsdk:"os_packages"`
}
Loading

0 comments on commit bc0d44b

Please sign in to comment.