Monday, February 1, 2021

Azure/Kubernetes: New-AzAksCluster contributed to Azure/azure-powershell (again) Documentation Fix

February 1, 2021 has been a productive day tweaking inconsequential issues in https://github.com/jdnark/azure-powershell. The examples in the documentation for New-AzAksCluster, https://docs.microsoft.com/en-us/powershell/module/az.aks/new-azakscluster?view=azps-5.4.0, used New-AzAks which is the legacy cmdlet. New-AzAksCluster is the cmdlet that is currently used to create a new Kubernetes cluster. New-AzAks is the alias for New-AzAksCluster so New-AzAksCluster should be used in the documentation examples: 



Azure/Kubernetes: contributed to Azure/azure-powershell

This was not the greatest contribution to the azure-powershell Git repository, https://github.com/jdnark/azure-powershell. The PR below fixes four instances of the same typo (changed SshKeyVaule to SshKeyValue) which manifests itself in the New-AzAksCluster cmdlet where a duplicate or missing SSH key is detected:





Sunday, January 31, 2021

Azure/PowerShell: Listing All Locations

Certain Azure PowerShell cmdlets require a location parameter value be specified (e.g. East Asia, West US 2, Brazil South, etc.). In order to display list of of locations the following PowerShell script can be invoked:

Get-AzureRmLocation | Select-Object -Property Location, DisplayName

The location parameter of a cmdlet should be passed the DisplayName of a given resource location. The below example demonstrates this where a new resource group is created for Norway East:

New-AzResourceGroup `
    -Name 'rg_ahha_take_on_me' `
    -Location 'Norway East' | Out-Null

Invoking the above script on January 31, 2020 listed the following locations:


Saturday, January 30, 2021

Azure: Installing Azure CLI on OS/X

Microsoft documents installing the Azure CLI on OS/X at Install Azure CLI on macOS but unfortunately following these instructions results in an error. The aforementioned documentation shows the following introduction and brew command to be used to install the AzureCLI on OS/X 


The error generated by using the above brew command to install the Azure CLI on OS/X is as follows:

This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!

The following command (removing the brew update &) install the Azure CLI on OS/X without error:

brew install azure-cli


Monday, January 25, 2021

C# 9.0: Record Types Non-Destructive Mutation (the With expression)

A C# 9.0 record type is immutable but there are times when it is useful to make a copy of a record type but only modify a subset of the properties. Under this scenario, the with express can be used with facilitates non-destructive mutation. To quote Steve Fenton's blog (C# 9 Non-Destructive Mutation): 


To demonstrate non-destructive mutation consider the following record type, a positional record (see C# 9.0: Positional Records):

public record Employee (
                  string Name, 
                  string Department,
                  string Office)
{
}    

The code below uses the with expression to make a copy of a record but allows specific properties to be changed in the newly created record and obviously does not modify the original record:


In the coding sample above, the first Employee record created is assigned to variable, victoria (line 13). The above code utilizes with expressions and behaves as follows:
  • Line 14: the victoria record instance is used to create a new employee, mario, but assigns a new value to the Name property, Mario Aguayo,
  • Line 15: the victoria record instance  is used to create a new employee, tarun, but assigns a new value to the Name property, Tarun Tiwari.
  • Line 16: the victoria record instance  is used to create a new employee, kai, but assigns a new value to the Name property, Kai Feng and Department property, QA.
  • Line 17: the kai record instance is used to create  a new employee, daisuke, but assigns a new value to the Name property, Daisuke Katchi and Office property, Via Carmen Suites.
 

Sunday, January 24, 2021

C# 9.0: Positional Records

This post introduces C# 9.0 positional records. Recall C# 9.0 record types were introduced in the post, C# 9.0: Record Types. A positional record is declared with a list of parameters such those shown below in lines 25 to line 29:


The record created contains an auto-property for each parameter in the declaration (PatientProperty, Count, Start, End, and DateRange). Each auto-property has a public access level. Auto-properties have a get and init access specifier (see: C# 9.0: Init Only Setters). The names of the parameters shown above were made uppercase so that the names of the auto-properties would be uppercase following the naming convention for properties.

Five parameters were specified in declaring the Patients record. The compiler creates a corresponding primary constructor taking the same five parameters as arguments. An example of the invocation of the primary constructor is as follows where the factory method, CreateDay, creates an instance of Patients where the parameters are passed at lines 43 through 47:


The auto-property, PatientProperty, is an enumeration also named PatientProperty. Use of the auto-property PatienProperty is shown below being used internally by the Patients class (lines 31, 33, and 35 below):


The PatientProperty property is never explicitly declared as all properties of the record corresponding to the declaration parameters are auto-properties and the compiler auto created each property. 

The PatientProperty enumeration is decorated by the Flags attribute (see FlagsAttribute Class) meaning that the enumerations values can be combined as they are bit fields. Lines 31, 33, and 35 above show the PatientProperty auto-property being used with the bitwise AND operator (&) in order to determine if the Patients in the date range are IsCovid19, IsIcu, or IsVentilator.

The source for this example is follows using screenshots from Visual Studio Code:

The source code in text form is found in Appendix A; Source Code at the end of this post.

Default Constructor

There is no default (parameterless) constructor created by the compiler for a positional record. It is illegal to create a parameterless constructor for a positional record. Below is an example (which will not compile) where the parameterless constructor is created and invoked at lines 47 to 53 using nominal object creation:


The error created while compiling the code above using Visual Studio Code is as  follows:

Appendix A: Source Code

The source code in its entirety is as shown below and of note:

  • There is no constructor explicitly declared for the class as the primary constructor is created by the compiler based on the parameters specified when the record was declared. 
  • The five parameters specified when the record was declared do not have explicit property declarations as the compiler automatically create this properties (auto-properties).
  • Other properties can be declared such as IsIcu, IsCovid19, and IsVentilator. The values for these parameters are computed using auto-properties assigned based on the parameters passed to the primary constructor. 
  • There is no implicit default constructor created for the record.

using System;

namespace HealthDashboard.Models 
{
    public enum DateRangeType 
    {
        Day,
        Week,
        Month,
        Quarter,
        Biannual,
        Year,
        Custom
    }

    [Flags]
    public enum PatientProperty
    {
        Covid19 = 0x1,
        Icu = 0x2,
        Ventilator = 0x4
    }

    public record Patients(
        PatientProperty PatientProperty,
        int Count,
        DateTime Start,
        DateTime End,
        DateRangeType DateRange) 
    {
        public bool IsCovid19 => 
           (0 != (PatientProperty & PatientProperty.Covid19));

        public bool IsIcu  => 
            (0 != (PatientProperty & PatientProperty.Icu));

        public bool IsVentilator => 
             (0 != (PatientProperty & PatientProperty.Ventilator));

        public static Patients CreateDay(
                    PatientProperty patientProperty, 
                    int count, 
                    DateTime start)
        {
            return new (
                    patientProperty,
                    count, 
                    start, 
                    start.AddDays(1), 
                    DateRangeType.Day);
        }                            

        public static Patients CreateWeek(
                    PatientProperty patientProperty, 
                    int count, 
                    DateTime start)
        {
            return new (
                    patientProperty,
                    count, 
                    start, 
                    start.AddDays(7), 
                    DateRangeType.Week);
        }                            

        public static Patients CreateMonth(
                    PatientProperty patientProperty, 
                    int count, 
                    DateTime start)
        {
            return new (
                    patientProperty,
                    count, 
                    start, 
                    start.AddMonths(1), 
                    DateRangeType.Month);
        }                           

        public static Patients CreateQuarter(
                    PatientProperty patientProperty, 
                    int count, 
                    DateTime start)
        {
            return new (
                    patientProperty,
                    count, 
                    start, 
                    start.AddMonths(3), 
                    DateRangeType.Quarter);
        }                            

        public static Patients CreateBiannual(
                    PatientProperty patientProperty, 
                    int count, 
                    DateTime start)
        {
            return new (
                    patientProperty,
                    count, 
                    start, 
                    start.AddMonths(6), 
                    DateRangeType.Biannual);
        }                            

        public static Patients CreateYear(
                    PatientProperty patientProperty, 
                    int count, 
                    DateTime start)
        {
            return new (
                    patientProperty,
                    count, 
                    start, 
                    start.AddYears(1), 
                    DateRangeType.Year);
        }                            

        public static Patients CreateCustom(
                    PatientProperty patientProperty, 
                    int count, 
                    DateTime start,
                    DateTime end)
        {
            return new (
                    patientProperty,
                    count, 
                    start, 
                    end, 
                    DateRangeType.Custom);
       }    
    }
}







Saturday, January 23, 2021

C# 9.0: Record Types

C# 9.0 introduces a read-only object type created using the record keyword versus the keywords struct or class. This post introduces record instances created using positional object creation (recall C# Nominal versus Positional Object Create). A future post will introduce creating record instances defined using nominal syntax, demonstrate declaring record types with positional syntax, and show the use of the with keyword in conjunction with record types.

Up until C# 9.0 objects were defined using the class (reference type) or struct (value type) keywords. Such objects are typical mutable. It should also be recognized that anonymous types are reference types and tuples are value types, but objects are explicitly defined using the class and struct keywords. 

The record keyword, to no surprise, defines an object of type record where record instances are immutable, reference object types. It is possible to define immutable types using class and struct such as the System.String class. The elegance of the record keyword is that it is clear that an instance of a record is immutable and the compiler creates constructs that support immutability that are built into each record instance. The following sections of this post expand on these ideas:

  • Appendix A: Comparing Record Type Instances
  • Appendix B: Record Type Compiler Synthesized Methods

The C# record to be implemented will be used to track patient hospitalization for a heath dashboard. That is certainly information that is important to provide but should also be immutable. The properties exposed by the Patients record are as follows:

  • IsCovid19: true if patients are Covid-19 positive
  • IsIcu: true if patients are in the I.C.U.
  • IsVentilator: true if patients use ventilators.
  • Count; number of patients
  • Start: patient information start date
  • End: patient information end date
  • RangeType: type of patient information date range (day, week, month, quarter, biannual, year or custom)

The DataRangeType enumeration is used to specify the RangeType property of the Patients record:


The code for the Patients record is shown below. Some things to note with regards to the code:

  • There is no set for each property (lines 18 to 30) as the properties can only be assigned at initialization. The init keyword could also be explicitly specified (see C# 9.0: Init Only Setters).
  • Fit and Finish (discussed in C# 9.0: Fit and Finish) is used at lines 50, 60, 70, 90, 100, and 111.
  • All source code below is provide in text form in Appendix C: Source Code.



Assigning a value to a Property After Instantiation

The code snippet below results in a complication error as it is a compile time error to modify a field or property of a record after it is instantiated:


The compiler errors from Visual Studio Code caused by line 116 (above) are as follows:



Appendix A: Comparing Record Type Instances

Microsoft's documentation on record types, Create Record Types, provides an excellent explanation of how record type instances are compared:

Appendix B: Record Type Compiler Synthesized Methods

Microsoft's overview of C# 9.0 features contains a comprehensive overview of record types (see What's new in C# 9.0: Record Types). To quote Microsoft's documentation on records: 

Appendix C: Source Code

using System;

namespace HealthDashboard.Models 
{
    public enum DateRangeType 
    {
        Day,
        Week,
        Month,
        Quarter,
        Biannual,
        Year,
        Custom
    }

    public record Patients 
    {
        public bool IsCovid19 { get; }

        public bool IsIcu { get; }    

        public bool IsVentilator { get; }    

        public int Count { get; }

        public DateTime Start { get; }   

        public DateTime End { get; }   

        public DateRangeType RangeType { get; }

        public Patients(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start,
                    DateTime end,
                    DateRangeType rangeType) => 
                        (Count, IsCovid19, IsIcu, IsVentilator, 
                                Start, End, RangeType) =
                            (count, isCovid19, isIcu, isVentilator, 
                                start.Date, end.Date, rangeType);

        public static Patients CreateDay(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start)
        {
            return new(count, isCovid19, isIcu, isVentilator, start, 
                       start.AddDays(1), DateRangeType.Day);
        }                  
          
        public static Patients CreateWeek(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start)
        {
            return new(count, isCovid19, isIcu, isVentilator, start, 
                       start.AddDays(7), DateRangeType.Week);
        }             
               
        public static Patients CreateMonth(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start)
        {
            return new(count, isCovid19, isIcu, isVentilator, start, 
                       start.AddMonths(1), DateRangeType.Month);
        }              
             
        public static Patients CreateQuarter(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start)
        {
            return new(count, isCovid19, isIcu, isVentilator, start, 
                       start.AddMonths(3), DateRangeType.Quarter);
        }              
              
        public static Patients CreateBiannual(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start)
        {
            return new(count, isCovid19, isIcu, isVentilator, start, 
                       start.AddMonths(6), DateRangeType.Biannual);
        }              
              
        public static Patients CreateYear(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start)
        {
            return new(count, isCovid19, isIcu, isVentilator, start, 
                       start.AddYears(1), DateRangeType.Year);
        }              
              
        public static Patients CreateCustom(
                    int count, 
                    bool isCovid19, 
                    bool isIcu, 
                    bool isVentilator,
                    DateTime start,
                    DateTime end)
        {
            return new(count, isCovid19, isIcu, isVentilator, start, 
                       end, DateRangeType.Custom);
        }                            
    }
}